【Android】学习笔记(3)——基本控件三

RadioGroup与RadioButton 控件
首先须要在布局文件中设置RadioGroup的属性,而后在该RadioGroup中添加RadioButton的属性。这也能够认为,RadioGroup是RadioButton的一个容器,首先创建容器,而后在容器中添加物体。
代码片断以下:
 
  
  1. <RadioGroupandroid

  2. android:id="@+id/gender"//设置id app

  3. android:layout_width="fill_parent"ide

  4. android:layout_height="wrap_content"布局

  5. android:orientation="vertical"//设置RadioGroup中RadioButton的排列方向 this

  6. >spa

  7. <RadioButtonxml

  8. android:id="@+id/male"//设置id 对象

  9. android:layout_width="fill_parent"blog

  10. android:layout_height="wrap_content"事件

  11. android:text="@string/male"

  12. />

  13. <RadioButton

  14. android:id="@+id/female"//设置id

  15. android:layout_width="fill_parent"

  16. android:layout_height="wrap_content"

  17. android:text="@string/female"

  18. />

  19. </RadioGroup>

布局文件写完以后,在所在Activity类中添加RadioGroup和RadioButton的对象。
由于是单选的,因此这个控件的事件须要设置在RadioGroup上,可使用RadioGroup的 setOnCheckedChangeListener的方法添加RadioGroup的 OnCheckedChangeListener的监听器,须要Override其中的 onCheckedChanged(RadioGroup group, int checkedId)方法,参数很明显,是所在group对象和所改变的RadioButton的id,在方法内部能够对RadioGroup以及RadioButton进行操做。
代码片断以下:
 
  
  1. RadioGroup genderGroup = (RadioGroup)findViewById(R.id.gender);

  2. RadioButton maleButton = (RadioButton)findViewById(R.id.male);

  3. maleButton.setChecked(true);//默认选择男

  4. RadioButton femaleButton = (RadioButton)findViewById(R.id.female);

  5. genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

  6. @Override

  7. publicvoid onCheckedChanged(RadioGroup group, int checkedId) {

  8. // TODO Auto-generated method stub

  9. if(femaleButton.getId()==checkedId){

  10. Toast.makeText(ControlDemo2Activity.this, "Female", Toast.LENGTH_SHORT).show();

  11. }

  12. else{

  13. Toast.makeText(ControlDemo2Activity.this, "Male", Toast.LENGTH_SHORT).show();

  14. }

  15. }

  16. });

运行效果:

233334388.png


CheckBox控件
首先要在布局文件中定义CheckBox的样式id等信息,每个CheckBox都须要定义。
代码片断以下:
 
  
  1. <CheckBox

  2. android:id="@+id/apple"

  3. android:layout_width="fill_parent"

  4. android:layout_height="wrap_content"

  5. android:text="@string/apple"

  6. />

  7. <CheckBox

  8. android:id="@+id/orange"

  9. android:layout_width="fill_parent"

  10. android:layout_height="wrap_content"

  11. android:text="@string/orange"

  12. />

  13. <CheckBox

  14. android:id="@+id/mango"

  15. android:layout_width="fill_parent"

  16. android:layout_height="wrap_content"

  17. android:text="@string/mango"

  18. />

定义完CheckBox的以后,在Activity中建立CheckBox的对象,每一个CheckBox上均可以设置监听器,可使用CheckBox的 setOnCheckedChangeListener的方法添加CompoundButton的 OnCheckedChangeListener的监听器,须要Override其中的 onCheckedChanged(CompoundButton arg0, boolean arg1),第一个参数是你所点击的CheckBox的对象,第二个参数是该对象是否被选中的boolean值,在方法中能够本身定义一些功能效果。
代码片断以下:
 
   
  1. CheckBox appleCheck = (CheckBox)findViewById(R.id.apple);

  2. appleCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

  3. @Override

  4. publicvoid onCheckedChanged(CompoundButton arg0, boolean arg1) {

  5. // TODO Auto-generated method stub

  6. if(arg1==true)//当该Checkbox被按下

  7. Toast.makeText(ControlDemo2Activity.this, "Apple Checked", Toast.LENGTH_SHORT).show();

  8. else

  9. Toast.makeText(ControlDemo2Activity.this, "Apple Unchecked", Toast.LENGTH_SHORT).show();

  10. }

  11. });

运行效果:

233746589.png

以上的两种控件的样式是能够在布局文件或者对象中能够设置。

附件为示例代码,仅供参考。嘎嘎~

若是个人文章给与了你帮助,就不妨请我喝杯咖啡吧,点击-> btn-index.png
相关文章
相关标签/搜索