zoukankan      html  css  js  c++  java
  • RadioGroup and RadioButton CheckBox test

    If we want to build a program about RadioGroup and RadioButton ,

    we should do following works.

    First,create MainActivity.java,activity_main.xml

    then ,bind the MainActivity.java with activity_main.xml

    next,add the next code at activity_main.xml

    code:

     1 <RadioGroup
     2         android:id="@+id/rg1"
     3         android:layout_width="wrap_content"
     4         android:layout_height="wrap_content" >
     5     
     6 
     7     <RadioButton
     8         android:id="@+id/male"
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:layout_alignLeft="@+id/rg1"
    12         android:layout_below="@+id/rg1"
    13         android:layout_marginTop="25dp" 
    14         android:text="@string/male"/>
    15 
    16     <RadioButton
    17         android:id="@+id/female"
    18         android:layout_width="wrap_content"
    19         android:layout_height="wrap_content"
    20         android:layout_alignLeft="@+id/male"
    21         android:layout_below="@+id/male" 
    22         android:text="@string/female"/>
    23     </RadioGroup>
    View Code

    in order to choose only one RadioButton we must put those RadioButton in the same group.

    next,

    in the MainActivity.java ,we should get the radiogroup by "radiogroup=(RadioGroup)findViewById(R.id.rg1);"

    and get the RadioButtons by

    female=(RadioButton)findViewById(R.id.female);
    male=(RadioButton)findViewById(R.id.male );

    next,

    use the radiogroup to create the listener.radiogroup.setOnCheckdchangeListener(new RadioGroup.OnCheckedListener(){

    @override

    public void onCheckedChanged(RadioGroup group, int checkedId){

    if (female.GetId()==checkedId)

    {

    //do your work

    }

    else

    {

    //do your work

    }

    }

    }

    );

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    CheckBox的使用

    checkbox 的使用和radiobutton的又不同。下面看怎么使用checkbutton。

    1.获得checkbutton对象run=(CheckBox)findViewById(R.id.run);

     1     run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     2             
     3             @Override
     4             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     5                 // TODO Auto-generated method stub
     6                 if (isChecked)
     7                 {
     8                     System.out.println("run is checked!");
     9                 }
    10                 else 
    11                 {
    12                     System.out.println("run is not checked!");
    13                 }
    14             }
    15         });

    在这是checkbox对象的 setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener())

    在重写方法的时候是在传进来的参数变成了Compoundbutton 对象,和ischecked 布尔值

    如果该checkbox对象被选中,那么ischecked为true 否则false

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  • 相关阅读:
    学号20155308 2016-2017-2 《Java程序设计》第6周学习总结
    学号20155308 2016-2017-2 《Java程序设计》第5周学习总结
    # 学号20155308 2006-2007-2 《Java程序设计》第4周学习总结
    学号20155308 2006-2007-2 《Java程序设计》第3周学习总结
    第二周作业--20155308郝文菲
    20155308郝文菲
    20155308郝文菲--第三次作业
    郝文菲get技能的经验--20155308
    第一篇博客
    20155302杨效宸第三次随笔
  • 原文地址:https://www.cnblogs.com/yinyakun/p/3164673.html
Copyright © 2011-2022 走看看