|
RadioButton ID
扩展学习
RadioGroup可动态配置其Layout,方法如下:
java代码:
LinearLayout.LayoutParams lp = new RadioGroup.LayoutParams ( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT );
复制代码
若欲利用程序动态创建RadioGroup以及RadioButton,方法如下:
Java代码:
/* 动态创建RadioGroup */
RadioGroup mRadioGroup01 = new RadioGroup(this);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams ( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT ); mRadioGroup01.setLayoutParams(layoutParams);
this.addContentView(mRadioGroup01, layoutParams);
/* 动态创建RadioButton 1 */
RadioButton mRadioButton01 = new RadioButton(this);
mRadioButton01.setText("选项1");
RadioGroup.LayoutParams layout1 = new RadioGroup.LayoutParams ( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT ); mRadioGroup01.addView(mRadioButton01, 0, layout1);
/* 动态创建RadioButton 2 */
RadioButton mRadioButton02 = new RadioButton(this);
mRadioButton02.setText("选项2");
RadioGroup.LayoutParams layout2 = new RadioGroup.LayoutParams ( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT ); mRadioGroup01.addView(mRadioButton02, 1, layout2);
复制代码
最后要介绍的是当User选择错误时,也可以在Button.OnClickListener里判断后,显示Animation,摇晃按钮,只要编写类似如下的代码段即可。
Java代码:
private Button.OnClickListener mChoose = new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mUserChoice) {
/* User答对了,程序略 */
} else {
Animation shake = AnimationUtils.loadAnimation (EX04_25.this, R.anim.shake);
v.startAnimation(shake);
}
}
};
复制代码
系列之Android 猜猜我谁(Game1)的帖子链接http://www.eoeandroid.com/thread-73563-1-1.html |
|