RadioButton、CheckBox与ToggleButton

本文介绍了如何在Android应用中使用RadioButton和ToggleButton控件实现功能选择,包括单选功能和开关功能的实现,以及如何通过监听事件控制UI元素的变化。

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文件

 

转载于:https://www.cnblogs.com/ggz19/p/3812217.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)数学优化概率(MOP)动态控制搜索过程,在全局探索局部开发之间实现平衡。文章详细解析了算法的初始化、勘探开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化并行计算等改进策略。; 适合人群:具备一定Python编程基础优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOAMOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值