XML代码:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lotus.my.MainActivity"> <RelativeLayout android:layout_width="399dp" android:layout_height="589dp" tools:layout_editor_absoluteX="6dp" tools:layout_editor_absoluteY="7dp"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/title" android:textColor="#e311c4ff" android:textSize="80px" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" tools:layout_constraintBottom_creator="1" tools:layout_constraintLeft_creator="1" tools:layout_constraintRight_creator="1" tools:layout_constraintTop_creator="1" /> <ImageView android:id="@+id/pic1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/title" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" app:srcCompat="@drawable/yihuo" /> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignStart="@+id/pic1" android:layout_below="@+id/pic1" android:layout_marginTop="48dp" android:text="@string/bt1" /> <Button android:id="@+id/bt3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bt3" android:layout_alignBaseline="@+id/bt2" android:layout_alignBottom="@+id/bt2" android:layout_alignStart="@+id/bt4" /> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bt2" android:layout_marginTop="21dp" android:layout_below="@+id/bt1" android:layout_toStartOf="@+id/bt3" android:layout_marginEnd="10dp" /> <Button android:id="@+id/bt4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/bt1" android:layout_alignBottom="@+id/bt1" android:layout_alignEnd="@+id/pic1" android:text="@string/bt4" /> <TextView android:id="@+id/coin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="38dp" /> </RelativeLayout> </android.support.constraint.ConstraintLayout>Java代码:
package com.example.lotus.my; import android.animation.ObjectAnimator; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ImageView pic; Button bbt1,bbt2,bbt3,bbt4; TextView c1; int c=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pic=(ImageView)findViewById(R.id.pic1); c1=(TextView)findViewById(R.id.coin); c1.setText("您的可用余额为:"+c+"元"); bbt1=(Button)findViewById(R.id.bt1); bbt2=(Button)findViewById(R.id.bt2); bbt3=(Button)findViewById(R.id.bt3); bbt4=(Button)findViewById(R.id.bt4); bbt1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ if (c>0){ AnimationSet xz=new AnimationSet(true); RotateAnimation rotateAnimation=new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5F,Animation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setDuration(100); rotateAnimation.setRepeatCount(-1); xz.addAnimation(rotateAnimation); pic.startAnimation(xz); c=c-1; c1.setText("您的可用余额为:"+c+"元");}else{Toast.makeText(getApplicationContext(),"请充值",Toast.LENGTH_LONG).show(); final EditText cz=new EditText(MainActivity.this); new AlertDialog.Builder(MainActivity.this).setTitle("充值").setIcon(R.drawable.yihuo).setView(cz) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Integer.parseInt(cz.getText().toString())>0){String text=cz.getText().toString();c=Integer.parseInt(text);c1.setText("您的可用余额为:"+c+"元");} else { Toast.makeText(getApplicationContext(),"请填写大于0的金额",Toast.LENGTH_LONG).show(); } } } ).setNegativeButton("取消",null).show();} } }); bbt4.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ if (c>0){ pic.clearAnimation(); c=c-1; c1.setText("您的可用余额为:"+c+"元");}else{Toast.makeText(getApplicationContext(),"请充值",Toast.LENGTH_LONG).show(); final EditText cz=new EditText(MainActivity.this); new AlertDialog.Builder(MainActivity.this).setTitle("充值").setIcon(R.drawable.yihuo).setView(cz) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Integer.parseInt(cz.getText().toString())>0){String text=cz.getText().toString();c=Integer.parseInt(text);c1.setText("您的可用余额为:"+c+"元");} else { Toast.makeText(getApplicationContext(),"请填写大于0的金额",Toast.LENGTH_LONG).show(); } } } ).setNegativeButton("取消",null).show();} } }); bbt2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ if (c>0){ ObjectAnimator xs=ObjectAnimator.ofFloat(pic,"alpha",1.0F,0.0F); xs.setDuration(1500); xs.start(); bbt3.setEnabled(true); c=c-2; c1.setText("您的可用余额为:"+c+"元");}else{Toast.makeText(getApplicationContext(),"请充值",Toast.LENGTH_LONG).show(); final EditText cz=new EditText(MainActivity.this); new AlertDialog.Builder(MainActivity.this).setTitle("充值").setIcon(R.drawable.yihuo).setView(cz) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Integer.parseInt(cz.getText().toString())>0){String text=cz.getText().toString();c=Integer.parseInt(text);c1.setText("您的可用余额为:"+c+"元");} else { Toast.makeText(getApplicationContext(),"请填写大于0的金额",Toast.LENGTH_LONG).show(); } } } ).setNegativeButton("取消",null).show();} } }); bbt3.setEnabled(false); bbt3.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ if (c>0){ ObjectAnimator cx=ObjectAnimator.ofFloat(pic,"alpha",0.0F,1.0F); cx.setDuration(1500); cx.start(); bbt3.setEnabled(false); c=c-2; c1.setText("您的可用余额为:"+c+"元");}else{Toast.makeText(getApplicationContext(),"请充值",Toast.LENGTH_LONG).show(); final EditText cz=new EditText(MainActivity.this); new AlertDialog.Builder(MainActivity.this).setTitle("充值").setIcon(R.drawable.yihuo).setView(cz) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Integer.parseInt(cz.getText().toString())>0){String text=cz.getText().toString();c=Integer.parseInt(text);c1.setText("您的可用余额为:"+c+"元");} else { Toast.makeText(getApplicationContext(),"请填写大于0的金额",Toast.LENGTH_LONG).show(); } } } ).setNegativeButton("取消",null).show();} } }); } }