android sdk --- radioGroup的选择保存学习以及animation学习

需求:

点击某项,选择answer,会反馈是否正确,错误的时候,按钮左右颤动;

选择clean,会把选择清空;

 

效果:

代码:

源码:

public class MainActivity extends Activity {

 protected Button mButton1,mButton2;
 protected TextView mTextView;
 protected RadioGroup mRadioGroup;
 protected boolean mUserChoice = false;
 protected int myChoice;
 protected int intTimes =0;
 protected RadioButton mRadio1,mRadio2,mRadio3;
 
 
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mButton1 = (Button)findViewById(R.id.button1);
  mButton2 = (Button)findViewById(R.id.button2);
  mTextView = (TextView)findViewById(R.id.textView1);
  
  mRadioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
  mRadio1 = (RadioButton)findViewById(R.id.radio0);
  mRadio2 = (RadioButton)findViewById(R.id.radio1);
  mRadio3 = (RadioButton)findViewById(R.id.radio2);
  
  int[] anyChoose =
   {
    mRadio1.getId(),mRadio2.getId(),mRadio3.getId(),
   };
  
  int intRandom = (int) (Math.random() * 3);
  myChoice=anyChoose[intRandom];
  

/*分离实现*/
mButton1.setOnClickListener(mChoose);
  mButton2.setOnClickListener(mClear);
  mRadioGroup.setOnCheckedChangeListener(mChangeRadio);
  
 }
 
 private RadioGroup.OnCheckedChangeListener mChangeRadio =
   new RadioGroup.OnCheckedChangeListener() {
    
    @Override
    public void onCheckedChanged(RadioGroup arg0, int arg1) {
     // TODO Auto-generated method stub
     if(arg1 == myChoice)
     {
      mUserChoice = true;
     }
     else
     {
      mUserChoice = false;
     }
    }
   };

 private Button.OnClickListener mChoose =
   new Button.OnClickListener()
 {
  public void onClick(View v)
  {
   if(mUserChoice)
   {
    mUserChoice  = false;
    intTimes = 0;

/*以下是为了给myChoice重新赋值*/
    mRadio1 = (RadioButton)findViewById(R.id.radio0);
    mRadio2 = (RadioButton)findViewById(R.id.radio1);
    mRadio3 = (RadioButton)findViewById(R.id.radio2);
    
    int[] anyChoose =
     {
      mRadio1.getId(),mRadio2.getId(),mRadio3.getId(),
     };
    
    int intRandom = (int) (Math.random() * 3);
    myChoice=anyChoose[intRandom];


    mRadioGroup.clearCheck();
    mTextView.setText("you are right !~congratulations!");
   }
   else
   {
    intTimes++;
    mTextView.setText("wrong times:"+Integer.toString(intTimes));
    

/*按钮的动画*/
    Animation shake  = AnimationUtils.loadAnimation(MainActivity.this, R.anim.shake);
    v.startAnimation(shake);
   }
  }
 };
 
 private Button.OnClickListener mClear =
   new Button.OnClickListener()
 {
  public void onClick(View v)
  {
    mUserChoice  = false;
    intTimes = 0;
    mRadio1 = (RadioButton)findViewById(R.id.radio0);
    mRadio2 = (RadioButton)findViewById(R.id.radio1);
    mRadio3 = (RadioButton)findViewById(R.id.radio2);
    
    int[] anyChoose =
     {
      mRadio1.getId(),mRadio2.getId(),mRadio3.getId(),
     };
    
    int intRandom = (int) (Math.random() * 3);
    myChoice=anyChoose[intRandom];
    mRadioGroup.clearCheck();
   
    mTextView.setText(" clear all");
   }
  };

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

 

xml:

shake.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0"
   
android:toXDelta="10"

android:duration="1000"

android:interpolator="@anim/cycle"

/>

cycle.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:cycles = "7" />

 

重点:

1.分离按钮的点击触发动作;

2.动画的实现;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值