zoukankan      html  css  js  c++  java
  • RadioButton、CheckBox与ToggleButton

    1.RadioButton

    RadioButton被称作为单选框,通常都是以组的形式出现,可以在一组控件中选择一个。

    RadioButton的使用首先需要加入<RadioGroup/>,在这个组中,我们进行单选按钮的声明。

     1   <RadioGroup
     2         android:id="@+id/radioGroup"
     3         android:layout_width="wrap_content"
     4         android:layout_height="wrap_content"
     5         android:layout_x="51dp"
     6         android:layout_y="182dp" >
     7 
     8         <RadioButton
     9             android:id="@+id/radioButton2"
    10             android:layout_width="wrap_content"
    11             android:layout_height="wrap_content"
    12             android:layout_x="172dp"
    13             android:layout_y="181dp"
    14             android:text="关灯" />
    15 
    16         <RadioButton
    17             android:id="@+id/radioButton1"
    18             android:layout_width="wrap_content"
    19             android:layout_height="wrap_content"
    20             android:layout_x="36dp"
    21             android:layout_y="201dp"
    22             android:text="开灯" />
    23     </RadioGroup>
    RadioButton

    这里我们定义了两个RadioButton按钮,用来控制图片的切换,我们需要为RadioButton添加监听事件

     1 Button myButton;
     2     ImageButton myImg;
     3     TextView textView;
     4     ToggleButton myToggle;
     5     ImageView img;
     6     CheckBox myCheck;
     7 
     8     @Override
     9     protected void onCreate(Bundle savedInstanceState) {
    10         super.onCreate(savedInstanceState);
    11         setContentView(R.layout.activity_main);
    12         myButton=(Button)findViewById(R.id.button1);
    13         textView=(TextView)findViewById(R.id.text1);
    14         myToggle=(ToggleButton)findViewById(R.id.toggleButton1);
    15  myCheck=(CheckBox)findViewById(R.id.checkBox1);
    16     RadioButton radio=(RadioButton)findViewById(R.id.radioButton2);
    17     RadioButton radio1=(RadioButton)findViewById(R.id.radioButton1);
    18     radio1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    19         
    20         @Override
    21         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    22             // TODO 自动生成的方法存根
    23             setBulbState(isChecked);
    24         }
    25     });
    26     radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    27         
    28         @Override
    29         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    30             // TODO 自动生成的方法存根
    31             setBulbState(isChecked);
    32         }
    33     });
    34       
    35     }
    36     private void setBulbState(boolean isChecked) {
    37         // TODO 自动生成的方法存根
    38            img=(ImageView)findViewById(R.id.imageView1);
    39         
    40             img.setImageResource((isChecked)?R.drawable.bulbon:R.drawable.buldoff);
    41         
    42         
    43          RadioButton radio1=(RadioButton)findViewById(R.id.radioButton1);
    44          radio1=(RadioButton)findViewById(R.id.radioButton1);
    45          radio1.setChecked(isChecked);
    46     radio1=(RadioButton)findViewById(R.id.radioButton2);
    47          radio1.setChecked(!isChecked);
    48     
    49     }
    RadioButton监听

    这里我们通过findViewById()来获取控件,并实现了控件的监听 setonCheckedChangeListener;

    2.CheckBox

    CheckBox控件被称为复选框,我们通过判断控件的选中状态,控制图片的切换。在资源文件中添加两个String对象,分别对应checkbox的选中状态,checkbox可以在不同的状态显示不同的Text。

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
          
     myCheck=(CheckBox)findViewById(R.id.checkBox1);
        
            myCheck.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    // TODO 自动生成的方法存根
                    setBulbState(isChecked);
                }});
        }
        private void setBulbState(boolean isChecked) {
            // TODO 自动生成的方法存根
               img=(ImageView)findViewById(R.id.imageView1);
            
                img.setImageResource((isChecked)?R.drawable.bulbon:R.drawable.buldoff);
            
            
             myCheck=(CheckBox)findViewById(R.id.checkBox1);
             myCheck.setText((isChecked)?R.string.offn:R.string.onn);
             myCheck.setChecked(isChecked);
        }

    3.ToogleButton

    ToogleButton俗称开关控件,可以分别设置它的EditTextOn和EditTextOff两个状态下的文字,对于该控件也需要添加监听的事件,获取控件的状态。

     1  protected void onCreate(Bundle savedInstanceState) {
     2         super.onCreate(savedInstanceState);
     3         setContentView(R.layout.activity_main);
     4        
     5         myToggle=(ToggleButton)findViewById(R.id.toggleButton1);
     6  
     7         myToggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){
     8 
     9             @Override
    10             public void onCheckedChanged(CompoundButton buttonView,
    11                     boolean isChecked) {
    12                 // TODO 自动生成的方法存根
    13                 setBulbState(isChecked);
    14             }
    15 
    16 
    17     }
    18     private void setBulbState(boolean isChecked) {
    19         // TODO 自动生成的方法存根
    20            img=(ImageView)findViewById(R.id.imageView1);
    21         
    22             img.setImageResource((isChecked)?R.drawable.bulbon:R.drawable.buldoff);
    23         
    24          myToggle=(ToggleButton)findViewById(R.id.toggleButton1);
    25          myToggle.setChecked(isChecked);
    26     }
    ToogleButton控件

    4.Xml文件

    Xml前台设置文件如下:

     1 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" >
    10 
    11     <TextView
    12         android:id="@+id/text1"
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:text="@string/hello_world" />
    16 
    17     <Button
    18         android:id="@+id/button1"
    19         android:layout_width="180dp"
    20         android:layout_height="64dp"
    21         android:layout_x="45dp"
    22         android:layout_y="269dp"
    23         android:background="@drawable/btn01"
    24         android:text="Button" />
    25 
    26     <ImageButton
    27         android:id="@+id/imageButton1"
    28         android:layout_width="60dp"
    29         android:layout_height="wrap_content"
    30         android:layout_x="139dp"
    31         android:layout_y="399dp"
    32         android:background="@drawable/easyicon_net_24"
    33         android:src="@drawable/imgbutton" />
    34 
    35     <ToggleButton
    36         android:id="@+id/toggleButton1"
    37         android:layout_width="wrap_content"
    38         android:layout_height="wrap_content"
    39         android:layout_x="145dp"
    40         android:layout_y="211dp"
    41         android:text="ToggleButton"
    42         android:textOff="开灯"
    43         android:textOn="关灯" />
    44 
    45     <ImageView
    46         android:id="@+id/imageView1"
    47         android:layout_width="77dp"
    48         android:layout_height="77dp"
    49         android:layout_x="30dp"
    50         android:layout_y="84dp"
    51         android:src="@drawable/buldoff" />
    52     <CheckBox
    53         android:id="@+id/checkBox1"
    54         android:layout_width="wrap_content"
    55         android:layout_height="wrap_content"
    56         android:layout_x="164dp"
    57         android:layout_y="115dp"
    58         android:text="@string/onn" />
    59 
    60     <RadioGroup
    61         android:id="@+id/radioGroup"
    62         android:layout_width="wrap_content"
    63         android:layout_height="wrap_content"
    64         android:layout_x="51dp"
    65         android:layout_y="182dp" >
    66 
    67         <RadioButton
    68             android:id="@+id/radioButton2"
    69             android:layout_width="wrap_content"
    70             android:layout_height="wrap_content"
    71             android:layout_x="172dp"
    72             android:layout_y="181dp"
    73             android:text="关灯" />
    74 
    75         <RadioButton
    76             android:id="@+id/radioButton1"
    77             android:layout_width="wrap_content"
    78             android:layout_height="wrap_content"
    79             android:layout_x="36dp"
    80             android:layout_y="201dp"
    81             android:text="开灯" />
    82     </RadioGroup>
    83 
    84 </AbsoluteLayout>
    Xml文件
  • 相关阅读:
    0--分析JDK及其他开源框架的初衷
    2.2--RandomAccessFile实现类和它的关联类FileChannel
    2.1--RandomAccessFile实现类的关联类FileChannel类图
    2--IO包DataInput接口类图
    1--UML语言IO包组件图
    跟老齐学Python Django实战 5/n Second edition
    跟老齐学Python Django实战 3/n Second edition
    Vim
    跟老齐学Python Django实战 2/n Second edition
    跟老齐学Python Django实战 1/n Second edition
  • 原文地址:https://www.cnblogs.com/ggz19/p/3812217.html
Copyright © 2011-2022 走看看