您的位置:首页 > 移动开发 > Android开发

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

2014-09-02 09:39 555 查看
需求:

点击某项,选择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.动画的实现;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息