• 0112常用控件+RadioGroup+CheckBox+提示语用法TOAST


    <RadioGroup>单选按钮的组

    要确定哪些单选按钮<RadioButton>在同一个组中

    对单选按钮绑定的是RadioGroup而不是RadioButton

    输入new RadioGroup后自动生成匿名内部类

    genderGroup.setOnCheckedChangeListener(new

    RadioGroup.OnCheckedChangeListener() {

                               

                                @Override

                                public void onCheckedChanged(RadioGroup group,

    int checkedId) {

                                         // TODO Auto-generated method stub

                                         //1哪一组里面的控件被点击了将会传进去

                                         //2组里的哪一个RadioButton被点击了传进去,

    即用户所点击的id

                                         if(femaleButton.getId()==checkedId)

                                         {System.out.println("female");}

                                         else if (maleButton.getId()==checkedId)

                                         {System.out.println("male");}

                                        

                                                  

                                        

                                }

                       });

    <Checkbox>的使用

    不存在组的概念

    提示语用法TOAST

    Toast.makeText(RadioTest.this, "你已经选择了female", Toast.LENGTH_LONG).show();

     RadioTest.java

      1 package com.example.mars_activity07;
      2 
      3 import android.os.Bundle;
      4 import android.widget.CheckBox;
      5 import android.widget.CompoundButton;
      6 import android.widget.RadioButton;
      7 import android.widget.RadioGroup;
      8 import android.widget.Toast;
      9 import android.app.Activity;
     10 
     11 
     12 public class RadioTest extends Activity {
     13 
     14     private RadioGroup genderGroup;
     15     private RadioButton femaleButton;
     16     private RadioButton maleButton;
     17     private CheckBox swim;
     18     private CheckBox run;
     19     private CheckBox read;
     20     
     21     @Override
     22     protected void onCreate(Bundle savedInstanceState) {
     23         super.onCreate(savedInstanceState);
     24         setContentView(R.layout.radio);
     25         genderGroup=(RadioGroup)findViewById(R.id.genderGroup);
     26         femaleButton=(RadioButton)findViewById(R.id.femaleButton);
     27         maleButton=(RadioButton)findViewById(R.id.maleButton);
     28         swim=(CheckBox)findViewById(R.id.swim);
     29         run=(CheckBox)findViewById(R.id.run);
     30         read=(CheckBox)findViewById(R.id.read);
     31         //绑定监听器,这里需要绑定的是RadioGroup
     32         //匿名内部类
     33         //查阅api知道OncheckedChangeListener是RG一个接口
     34         genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
     35             
     36             @Override
     37             public void onCheckedChanged(RadioGroup group, int checkedId) {
     38                 // TODO Auto-generated method stub
     39                 //1哪一组里面的控件被点击了将会传进去
     40                 //2组里的哪一个RadioButton被点击了传进去,即用户所点击的id
     41                 if(femaleButton.getId()==checkedId)
     42                 {System.out.println("female");
     43                 Toast.makeText(RadioTest.this, "你已经选择了female", Toast.LENGTH_LONG).show();
     44                 }
     45                 else if (maleButton.getId()==checkedId)
     46                 {System.out.println("male");}
     47                 
     48                     
     49                 
     50             }
     51         });
     52         //为多选按钮添加监听器,与Radio是对一组绑定,而这个需要对每一个按钮绑定,他们的绑定方式与之前的button也不同
     53         swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     54             
     55             @Override
     56             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     57                 // TODO Auto-generated method stub
     58                 
     59                 if(isChecked)
     60                 {
     61                     System.out.println("swim");
     62                 }
     63                 
     64             }
     65         });
     66         
     67         run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     68             
     69             @Override
     70             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     71                 // TODO Auto-generated method stub
     72                 if(isChecked)
     73                 {
     74                     System.out.println("run");
     75                 }
     76                 
     77             }
     78         });
     79         
     80         read.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
     81             
     82             @Override
     83             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     84                 // TODO Auto-generated method stub
     85                 if(isChecked)
     86                 {
     87                     System.out.println("read");
     88                 }
     89                 
     90             }
     91         });
     92         
     93     
     94     
     95         
     96         
     97         
     98 
     99     
    100      
    101     }
    102 
    103     
    104     
    105 }


    radio.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6     <RadioGroup
     7         android:id="@+id/genderGroup"
     8         android:layout_height="wrap_content"
     9         android:layout_width="wrap_content"
    10         android:orientation="vertical">
    11         <RadioButton
    12             android:id="@+id/femaleButton"
    13             android:layout_height="wrap_content"
    14             android:layout_width="wrap_content"
    15             android:text="@string/female"/>
    16         <RadioButton
    17             android:id="@+id/maleButton"
    18             android:layout_height="wrap_content"
    19             android:layout_width="wrap_content"
    20             android:text="@string/male"/>
    21         <RadioButton
    22             android:id="@+id/uncertainButton"
    23             android:layout_height="wrap_content"
    24             android:layout_width="wrap_content"
    25             android:text="@string/uncertain"/>
    26         
    27         "
    28     </RadioGroup> 
    29     
    30     
    31     <CheckBox 
    32         android:id="@+id/swim"
    33         android:layout_height="wrap_content"
    34         android:layout_width="wrap_content"
    35         android:text="@string/swim"
    36         />
    37     
    38     
    39      <CheckBox 
    40         android:id="@+id/run"
    41         android:layout_height="wrap_content"
    42         android:layout_width="wrap_content"
    43         android:text="@string/run"
    44         />
    45     
    46      <CheckBox 
    47         android:id="@+id/read"
    48         android:layout_height="wrap_content"
    49         android:layout_width="wrap_content"
    50         android:text="@string/read"
    51         />
    52     
    53 </LinearLayout>
  • 相关阅读:
    WPF:DataGrid 自动生成行号
    C#:WinForm之Command
    i.MX RT1010之FlexIO模拟SSI外设
    i.MX RT1010之FlexIO模拟I2S外设
    i.MX RT600之DSP开发环境调试篇
    i.MX RT600之DSP调试环境搭建篇
    i.MX RT600之DMIC外设介绍及应用
    i.MX RT600之I2S外设介绍及应用
    ssm框架思维-登录
    idea里面搭建SSM框架-crud
  • 原文地址:https://www.cnblogs.com/kyxyes/p/2883673.html
Copyright © 2020-2023  润新知