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

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

  • 相关阅读:
    golang-switch结构辨析有话
    不用中间变量交换变量值-golang版
    vue element ui表单验证不通过,滚动到页面上第一个验证失败的输入框位置
    表单校验中使用v-if和v-else来判断是福哦要校验时的注意项
    如何修改本地项目关联的远程仓库地址
    vue-cli3如何访问public文件夹下的静态资源
    Git 命令行的各种退出方式
    elementui表格如何在表头每个列标题后面插入图片用于插入tooltip
    js 把一个二叉树类型的对象转化为普通对象
    element-ui树结构设置默认选中节点时改变传入的数组树结构没有变化
  • 原文地址:https://www.cnblogs.com/yinyakun/p/3164673.html
Copyright © 2011-2022 走看看