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.动画的实现;
点击某项,选择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.动画的实现;
相关文章推荐
- iOS-Animation学习2-CABasicAnimation以及CAAnimationGroup的基本用法
- RadioGroup、RadioButton以及Spinner学习笔记
- 敏捷开发学习总结(1):传统序列式软件开发方法的缺点,以及迭代开发方法的选择
- Win form 在pictureBox控件中实现选择图片然后以二进制方式保存到数据库。以及在pictureBox控件显示出来
- android 涂鸦(清屏,画笔,粗细,保存)以及canvas源码学习
- Android RadioGroup 学习
- Android RadioGroup 学习
- Tapestry学习十一:关于组件(六)Radio and RadioGroup
- 敏捷思维学习Ext.Net MVC--3.7Form表单组件之单选组件组(RadioGroup)
- 【Android开发学习26】界面控件之选项组件(RadioGroup和CheckBox)
- spring学习之springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
- Android学习自定义View(二)——View和ViewGroup绘制流程以及invalidate()
- API Demos 2.3 学习笔记 (15)-- Views->Radio Group
- 山寨开机选择四大系统-----------RadioGroup 、RadioButton单选按钮
- 关于Android中的radioGroup选择
- Android开发之四(五):常用控件之单项选择(RadioGroup和RadioButton)
- Android学习-----对RadioGroup和TabHost监听的理解
- 单项选择功能RadioGroup和RadioButton的使用
- flex 入门学习笔记3 之RadioButton和RadioButtonGroup
- python学习一:软件版本选择以及代码调试初探