android RadioButton和CheckBox組件的使用方法
RadioButton是單選按鈕,多個RadioButton放在一個RadioGroup控件中,也就是說每次只能有1個RadioButton被選中。而CheckBox是多選按鈕,Toatst是android中帶的一個用于顯示提示小窗口消息的控件,其提示的內容過一會兒會自動消失。
RadioGroup和CheckBox控件設置監(jiān)聽器都是用的setOnCheckedChangeListener函數(shù),其輸入?yún)?shù)是一個函數(shù),且函數(shù)內部要實現(xiàn)1個內部類。RadioGroup監(jiān)聽器的輸入?yún)?shù)用的是RadioGroup.OnCheckedChangeListener(),而CheckBox監(jiān)聽器的輸入?yún)?shù)用的是函數(shù)CompoundButton.OnCheckedChangeListener().
開發(fā)環(huán)境:android4.1
實驗效果如下(采用的是線性布局):
效果圖:
上面3個為一組RadioGroup,每選中其中一個RadioButton,則會有相應的提示。且只能選中其中的一個。
下面的4都為CheckBox,可以選中其中的多個。每個CheckBox被選中或者取消選中都有相應的文字提示小窗口。
代碼如下:
MainActivity.java:
package com.example.control1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
//定義各控件的變量
private TextView who = null;
private TextView how = null;
private RadioGroup who_group = null;
private RadioButton china = null;
private RadioButton america = null;
private RadioButton others = null;
private CheckBox less = null;
private CheckBox thirty = null;
private CheckBox forty = null;
private CheckBox fifty = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲得對應的控件
who = (TextView)findViewById(R.id.who);
how = (TextView)findViewById(R.id.how);
who_group = (RadioGroup)findViewById(R.id.who_group);
china = (RadioButton)findViewById(R.id.china);
america = (RadioButton)findViewById(R.id.america);
others = (RadioButton)findViewById(R.id.others);
less = (CheckBox)findViewById(R.id.less);
thirty = (CheckBox)findViewById(R.id.thirty);
forty = (CheckBox)findViewById(R.id.forty);
fifty = (CheckBox)findViewById(R.id.fifty);
//設置who_group的監(jiān)聽器,其實是一句代碼,其參數(shù)是一個帶有重構函數(shù)的對象
who_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == china.getId()){
Toast.makeText(MainActivity.this,"中國", Toast.LENGTH_SHORT).show();
}
else if(checkedId == america.getId()){
Toast.makeText(MainActivity.this, "美國", Toast.LENGTH_SHORT).show();
}
else if(checkedId == others.getId()){
Toast.makeText(MainActivity.this, "其它國家", Toast.LENGTH_SHORT).show();
}
}
});
//下面為4個checkbox多選按鈕分別建立監(jiān)聽器
less.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "30個以下", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "不是30個以下", Toast.LENGTH_SHORT).show();
}
}
});
//下面為4個checkbox多選按鈕分別建立監(jiān)聽器
thirty.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "30~39", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "不是30~39", Toast.LENGTH_SHORT).show();
}
}
});
//下面為4個checkbox多選按鈕分別建立監(jiān)聽器
forty.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "40~49", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "不是40~49", Toast.LENGTH_SHORT).show();
}
}
});
//下面為4個checkbox多選按鈕分別建立監(jiān)聽器
fifty.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "50以上", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "不是50以上", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/who"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/who"
/>
<RadioGroup
android:id="@+id/who_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/china"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/china"
/>
<RadioButton
android:id="@+id/america"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/america"
/>
<RadioButton
android:id="@+id/others"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/others"
/>
</RadioGroup>
<TextView
android:id="@+id/how"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/how"
/>
<CheckBox
android:id="@+id/less"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/less"
/>
<CheckBox
android:id="@+id/thirty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/thirty"
/>
<CheckBox
android:id="@+id/forty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/forty"
/>
<CheckBox
android:id="@+id/fifty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fifty"
/>
</LinearLayout>
實驗總結:通過本次實驗對RadioGroup,CheckBox,RadioButton和Toast這4個控件的簡單使用有了個初步的了解。
作者:tornadomeet
- Android單選按鈕RadioButton的使用詳解
- Android控件RadioButton實現(xiàn)多選一功能
- Android開發(fā)設置RadioButton點擊效果的方法
- Android編程實現(xiàn)自定義PopupMenu樣式示例【顯示圖標與設置RadioButton圖標】
- Android RadioButton 圖片位置與大小實例詳解
- Android RadioGroup和RadioButton控件簡單用法示例
- Android中設置RadioButton在文字右邊的方法實例
- Android RadioButton單選框的使用方法
- Android定制RadioButton樣式三種實現(xiàn)方法
- Android控件系列之RadioButton與RadioGroup使用方法
- Android控件RadioButton的使用方法
相關文章
Android selector狀態(tài)選擇器的使用詳解
這篇文章主要為大家詳細介紹了Android selector狀態(tài)選擇器的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09
Android基于Toolbar實現(xiàn)頂部標題欄及后退鍵
這篇文章主要介紹了Android基于Toolbar實現(xiàn)頂部標題欄及后退鍵,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09
Android開發(fā)中獲取View視圖寬與高的常用方法小結
這篇文章主要介紹了Android開發(fā)中獲取View視圖寬與高的常用方法,結合實例形式總結分析了Android獲取View視圖寬與高的三種常用方法及使用場景,需要的朋友可以參考下2017-10-10
Android Material Design 陰影實現(xiàn)示例
這篇文章主要介紹了Android Material Design 陰影實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
Android開發(fā)實現(xiàn)Launcher3應用列表修改透明背景的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)Launcher3應用列表修改透明背景的方法,結合實例形式分析了Launcher3相關配置文件與功能函數(shù)修改設置操作技巧,需要的朋友可以參考下2017-11-11

