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()

你可能感兴趣的:(Android中的控件--单选框,多选框,提示框)