Android中的多选框,提示框

本文详细介绍了Android开发中常用的UI控件,包括RadioGroup、RadioButton和CheckBox的使用方法及注意事项,并提供了具体的XML布局代码示例和Java代码示例,帮助开发者更好地理解和应用这些控件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android中的多选框,提示框

一、RadioGroup和RadioButton的使用方法【单选按钮】

在设计单选按钮时,要根据实际情况对按钮进行分组,因为在同一组中的单选按钮只有一个能被选中。

在程序代码中实现要分别RadioGroup和RadioButton进行声明。

用findViewById()方法来取得对应控件的对象。

绑定监听器,注意:是对RadioGroup进行绑定,SetOnCheckedChangeListener(new…..)


RadioGroup继承结构图:

java.lang.Object
   ↳android.view.View
    ↳android.view.ViewGroup
     ↳android.widget.LinearLayout
      ↳android.widget.RadioGroup

所以对于RadioGroup在布局文件xml中的属性依然有常见的:

android:id —-android.view.View

android:layout_width —-java.lang.Object

android:layout_height —-java.lang.Object

android:orientation —-android.widget.LinearLayout

RadioButton继承结构图:

java.lang.Object
   ↳android.view.View
    ↳android.widget.TextView
     ↳android.widget.Button
      ↳android.widget.CompoundButton
       ↳android.widget.RadioButton

所以对于RadioButton在布局文件xml中的属性依然有常见的:

android:id —-android.view.View

android:layout_width —-java.lang.Object

android:layout_height —-java.lang.Object

android:text —- android.widget.TextView

xml布局文件代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
<RadioButton
     android:id="@+id/femaleButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/female"      />
<RadioButton
     android:id="@+id/maleButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/male"      />
</RadioGroup>

监听器绑定方式

接口

RadioGroup.OnCheckedChangeListener 该接口定义一个回调函数,回调onCheckedChanged(RadioGroup group,int checkedId)方法,当radiobutton在这个组中被选中从而发生改变时被调用.
Public Methods

void

setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener)对于这个RadioGroup绑定一个监听器,当radiobutton在这个组中被选中从而发生改变时被调用.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private RadioGroup genderGroup = null;
private RadioButton femaleButton = null;
private RadioButton maleButton = null;
genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
femaleButton = (RadioButton)findViewById(R.id.femaleButton);
maleButton = (RadioButton)findViewById(R.id.maleButton);
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(femaleButton.getId() == checkedId){
        System.out.println("famale");
        Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();}
                else if(maleButton.getId() == checkedId)
                {System.out.println("male");}
            }
        });

二、CheckBox的使用方法【单选按钮】


CheckBox继承结构图

java.lang.Object
   ↳android.view.View
    ↳android.widget.TextView
     ↳android.widget.Button
      ↳android.widget.CompoundButton
       ↳android.widget.CheckBox

所以对于RadioGroup在布局文件xml中的属性依然有常见的:

android:id —-android.view.View

android:layout_width —-java.lang.Object

android:layout_height —-java.lang.Object

android:text —- android.widget.TextView

xml布局文件代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<CheckBox
    android:id="@+id/swim"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/swim"
    />
<CheckBox
    android:id="@+id/run"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/run"
    />
<CheckBox
    android:id="@+id/read"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/read"
    />

监听器绑定方式

接口

CompoundButton.OnCheckedChangeListener 该接口定义一个回调函数,回调onCheckedChanged(CompoundButton buttonView, boolean isChecked)方法,当Checkbox在这个组中被选中从而发生改变时被调用.
Public Methods

void

setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener)对于这个CheckBox绑定一个监听器,当CheckBox被选中从而发生改变时被调用.

在CompoundButton下有CheckBox, RadioButton, ToggleButton,对于RadioButton而言,是包含在RadioGroup群组下,

而且监听器是绑定在RadioGroup上,所以调用的是RadioGroup自身的监听接口。而CheckBox是在CompoundButton下,

且监听器是绑定在CheckBox自身上,所以调用的是CompoundButton中的监听接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private CheckBox readBox = null;
readBox = (CheckBox)findViewById(R.id.read);    
readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    System.out.println("read is checked");
                }
                else
                {
                    System.out.println("read is unchecked");
                }
        }
};

三、Toast的使用方法【单选按钮】


概述:

Toast是一种对用户的快速消息显示视图,当这种视图需要对用户显示是,会通过应用程序的浮动窗口形式来显示,

其浮动界面不接受焦点。有的时候用户可能会在指定输入的地方输入一些错误的信息,用户不一定轻易的察觉出来

应用程序就可以toast来提醒用户应该正确输入什么。

Toast继承结构图

java.lang.Object
   ↳android.widget.Toast

常用方法

Public Methods

static Toast

makeText(Context context, CharSequence text, int duration)Make a standard toast that just contains a text view.

void

show()Show the view for the specified duration.

1
Toast.makeText(radioTest.this,"famle",Toast.LENGTH_SHORT).show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值