android RadioButton和CheckBox組件的使用方法
RadioButton是單選按鈕,多個(gè)RadioButton放在一個(gè)RadioGroup控件中,也就是說(shuō)每次只能有1個(gè)RadioButton被選中。而CheckBox是多選按鈕,Toatst是android中帶的一個(gè)用于顯示提示小窗口消息的控件,其提示的內(nèi)容過(guò)一會(huì)兒會(huì)自動(dòng)消失。
RadioGroup和CheckBox控件設(shè)置監(jiān)聽(tīng)器都是用的setOnCheckedChangeListener函數(shù),其輸入?yún)?shù)是一個(gè)函數(shù),且函數(shù)內(nèi)部要實(shí)現(xiàn)1個(gè)內(nèi)部類(lèi)。RadioGroup監(jiān)聽(tīng)器的輸入?yún)?shù)用的是RadioGroup.OnCheckedChangeListener(),而CheckBox監(jiān)聽(tīng)器的輸入?yún)?shù)用的是函數(shù)CompoundButton.OnCheckedChangeListener().
開(kāi)發(fā)環(huán)境:android4.1
實(shí)驗(yàn)效果如下(采用的是線性布局):
效果圖:
上面3個(gè)為一組RadioGroup,每選中其中一個(gè)RadioButton,則會(huì)有相應(yīng)的提示。且只能選中其中的一個(gè)。
下面的4都為CheckBox,可以選中其中的多個(gè)。每個(gè)CheckBox被選中或者取消選中都有相應(yīng)的文字提示小窗口。
代碼如下:
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);
//獲得對(duì)應(yīng)的控件
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);
//設(shè)置who_group的監(jiān)聽(tīng)器,其實(shí)是一句代碼,其參數(shù)是一個(gè)帶有重構(gòu)函數(shù)的對(duì)象
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,"中國(guó)", Toast.LENGTH_SHORT).show();
}
else if(checkedId == america.getId()){
Toast.makeText(MainActivity.this, "美國(guó)", Toast.LENGTH_SHORT).show();
}
else if(checkedId == others.getId()){
Toast.makeText(MainActivity.this, "其它國(guó)家", Toast.LENGTH_SHORT).show();
}
}
});
//下面為4個(gè)checkbox多選按鈕分別建立監(jiān)聽(tīng)器
less.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "30個(gè)以下", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "不是30個(gè)以下", Toast.LENGTH_SHORT).show();
}
}
});
//下面為4個(gè)checkbox多選按鈕分別建立監(jiān)聽(tīng)器
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個(gè)checkbox多選按鈕分別建立監(jiān)聽(tīng)器
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個(gè)checkbox多選按鈕分別建立監(jiān)聽(tīng)器
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>
實(shí)驗(yàn)總結(jié):通過(guò)本次實(shí)驗(yàn)對(duì)RadioGroup,CheckBox,RadioButton和Toast這4個(gè)控件的簡(jiǎn)單使用有了個(gè)初步的了解。
作者:tornadomeet
- Android單選按鈕RadioButton的使用詳解
- Android控件RadioButton實(shí)現(xiàn)多選一功能
- Android開(kāi)發(fā)設(shè)置RadioButton點(diǎn)擊效果的方法
- Android編程實(shí)現(xiàn)自定義PopupMenu樣式示例【顯示圖標(biāo)與設(shè)置RadioButton圖標(biāo)】
- Android RadioButton 圖片位置與大小實(shí)例詳解
- Android RadioGroup和RadioButton控件簡(jiǎn)單用法示例
- Android中設(shè)置RadioButton在文字右邊的方法實(shí)例
- Android RadioButton單選框的使用方法
- Android定制RadioButton樣式三種實(shí)現(xiàn)方法
- Android控件系列之RadioButton與RadioGroup使用方法
- Android控件RadioButton的使用方法
相關(guān)文章
Android控件Tween動(dòng)畫(huà)(補(bǔ)間動(dòng)畫(huà))實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android控件Tween動(dòng)畫(huà)(補(bǔ)間動(dòng)畫(huà))實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了Android補(bǔ)間動(dòng)畫(huà)的原理、功能實(shí)現(xiàn)與布局相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Android開(kāi)發(fā)仿咸魚(yú)鍵盤(pán)DEMO(修改版)
本文給大家分享一段代碼關(guān)于android開(kāi)發(fā)高仿咸魚(yú)鍵盤(pán)修改版的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常實(shí)用,需要的朋友參考下吧2016-11-11Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法
這篇文章主要介紹了Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法,本文介紹了4個(gè)相關(guān)的方法,分別對(duì)它們做了講解,需要的朋友可以參考下2015-06-06Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間(示例代碼)
這篇文章主要介紹了Android動(dòng)態(tài)顯示當(dāng)前年月日時(shí)分秒系統(tǒng)時(shí)間的示例代碼,需要的朋友可以參考下2017-05-05Android EditText被軟鍵盤(pán)遮蓋的處理方法
android app新增了透明欄效果,結(jié)果發(fā)現(xiàn)鍵盤(pán)彈起后會(huì)遮蓋屏幕底部的EditText,沒(méi)有像想象中的調(diào)整窗口大小,并滾動(dòng)ScrollView,將EditText顯示在鍵盤(pán)上方。下面小編把解決方法記錄一下,特此分享到腳本之家平臺(tái),感興趣的朋友一起看看吧2016-10-10Android中判斷網(wǎng)絡(luò)連接是否可用的方法總結(jié)
這篇文章主要介紹了Android中判斷網(wǎng)絡(luò)連接是否可用的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04詳解Android應(yīng)用中preference首選項(xiàng)的編寫(xiě)方法
這篇文章主要介紹了Android應(yīng)用中preference首選項(xiàng)的編寫(xiě)方法,或許Apple將其翻譯為'偏好設(shè)置'更直觀些,即用戶對(duì)應(yīng)用的一些個(gè)性化調(diào)整菜單,需要的朋友可以參考下2016-04-04SurfaceView播放視頻發(fā)送彈幕并實(shí)現(xiàn)滾動(dòng)歌詞
這篇文章主要為大家詳細(xì)介紹了SurfaceView播放視頻發(fā)送彈幕并實(shí)現(xiàn)滾動(dòng)歌詞,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11