Android單選多選按鈕的使用方法
本文實(shí)例為大家分享了Android單選多選按鈕使用的具體代碼,供大家參考,具體內(nèi)容如下
一、單選按鈕
單選按鈕類:RadioButton
android:checked="true"設(shè)置默認(rèn)選中
單選按鈕控件通常與RadioGroup搭配使用。
- RadioGroup是LinearLayout的子類,用于將多個(gè)單選按鈕組合為一組。
- 同一按鈕組內(nèi)的單選按鈕只能有一個(gè)被選中。
二、多選按鈕
用法基本與Button相同
CheckBox對(duì)象.isChecked()方法可以用來(lái)判斷復(fù)選按鈕是否選中
效果圖(單選多選寫在一個(gè)項(xiàng)目里邊,用了一個(gè)頁(yè)面跳轉(zhuǎn)):
項(xiàng)目目錄:
多選按鈕,兩種形式
代碼:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:id="@+id/LinearLayout1" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? tools:context="${relativePackage}.${activityClass}" > ? ? ? <Button ? ? ? ? android:id="@+id/button1" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="RadioActivity單選" /> ? ? ? <Button ? ? ? ? android:id="@+id/button2" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="CheckActivity多選" /> ? </LinearLayout>
MainActivity.java
package com.example.radioandcheckdemo; ? import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; ? public class MainActivity extends Activity implements OnClickListener{ ? ?? ?private Button button1; ?? ?private Button button2; ?? ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ?? ? ? ? ? button1 = (Button) findViewById(R.id.button1); ? ? ? ? button2 = (Button) findViewById(R.id.button2); ? ? ? ? button1.setOnClickListener(this); ? ? ? ? button2.setOnClickListener(this); ? ? ? ?? ? ? } ? ?? ?@Override ?? ?public void onClick(View v) { ?? ??? ?Intent intent = new Intent(); ?? ??? ?switch (v.getId()) { ?? ??? ?case R.id.button1: ?? ??? ??? ?//跳轉(zhuǎn)頁(yè)面 ?? ??? ??? ?intent.setClass(MainActivity.this, RadioActivity.class); ?? ??? ??? ?startActivity(intent); ?? ??? ??? ?break; ?? ??? ?case R.id.button2: ?? ??? ??? ?//跳轉(zhuǎn)頁(yè)面 ?? ??? ??? ?intent.setClass(MainActivity.this, CheckActivity.class); ?? ??? ??? ?startActivity(intent); ?? ??? ?default: ?? ??? ??? ?break; ?? ??? ?} ?? ?} }
activity_radio.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:id="@+id/LinearLayout1" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? android:layout_margin="20sp" ? ? tools:context="${relativePackage}.${activityClass}" > ? ? ? <TextView ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="@string/hello_world" /> ? ? ? <!--? ? ? ?? ?單選 ? ? ?? ?android:checked="true"設(shè)置默認(rèn)選中 ? ? ?--> ? ? <RadioGroup ? ? ? ? android:id="@+id/group1" ? ? ? ? android:orientation="horizontal" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" > ? ? ? ?? ? ? ? ? <RadioButton? ? ? ? ? ? ? android:id="@+id/radio1" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:checked="true" ? ? ? ? ? ? android:text="男"/> ? ? ? ? ?<RadioButton? ? ? ? ? ? ? ?android:id="@+id/radio2" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="女"/> ? ? ? ?? ? ? </RadioGroup> ? ? ? <!-- 分界線 --> ? ? <View ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="2sp" ? ? ? ? android:background="@android:color/holo_blue_dark" ? ? ? ? android:layout_marginTop="10sp" ? ? ? ? android:layout_marginBottom="10sp" /> ? ?? ? ? <TextView? ? ? ? ? android:id="@+id/text1" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:textSize="18sp" ? ? ? ? android:text="你吃飯了嗎?"/> ? ? ? <RadioGroup ? ? ? ? android:id="@+id/group2" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" > ? ? ? ?? ? ? ? ? <RadioButton? ? ? ? ? ? ? android:id="@+id/radio3" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="吃了"/> ? ? ? ? ?<RadioButton? ? ? ? ? ? ? android:id="@+id/radio4" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="沒(méi)吃"/> ? ? ? ?? ? ? </RadioGroup> ? </LinearLayout>
RadioActivity.java
package com.example.radioandcheckdemo; ? import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; ? public class RadioActivity extends Activity implements OnCheckedChangeListener { ?? ?private RadioGroup group1; ?? ?private RadioGroup group2; ?? ?@Override ?? ?protected void onCreate(Bundle savedInstanceState) { ?? ??? ?super.onCreate(savedInstanceState); ?? ??? ?setContentView(R.layout.activity_radio); ?? ??? ? ?? ??? ?group1 = (RadioGroup) findViewById(R.id.group1);? ?? ??? ?group2 = (RadioGroup) findViewById(R.id.group2);? ?? ??? ?group1.setOnCheckedChangeListener(this); ?? ??? ?group2.setOnCheckedChangeListener(this); ?? ?} ?? ? ?? ?@Override ?? ?public void onCheckedChanged(RadioGroup group, int checkedId) { ?? ??? ?//顯示值的幾種方法 ?? ??? ? ?? ??? ?//checkedId選中RadioButton的id ?? ??? ?/*switch (checkedId) { ?? ??? ?case R.id.radio1: ?? ??? ??? ?Toast.makeText(this, "男", Toast.LENGTH_LONG).show(); ?? ??? ??? ?break; ?? ??? ?case R.id.radio2: ?? ??? ??? ?Toast.makeText(this, "女", Toast.LENGTH_LONG).show(); ?? ??? ??? ?break; ?? ??? ?case R.id.radio3: ?? ??? ??? ?Toast.makeText(this, "吃了", Toast.LENGTH_LONG).show(); ?? ??? ??? ?break; ?? ??? ?case R.id.radio4: ?? ??? ??? ?Toast.makeText(this, "沒(méi)吃", Toast.LENGTH_LONG).show(); ?? ??? ??? ?break; ?? ??? ?default: ?? ??? ??? ?break; ?? ??? ?}*/ ?? ??? ? ?? ??? ?//找到點(diǎn)擊的RadioButton ?? ??? ?//RadioButton radio = (RadioButton) findViewById(checkedId); ?? ??? ?//取出RadioButton中的值 ?? ??? ?//String str = radio.getText().toString(); ?? ??? ?//彈框顯示選中的值 ?? ??? ?//Toast.makeText(this, str, Toast.LENGTH_LONG).show(); ?? ??? ? ?? ??? ?//兩組數(shù)據(jù)同時(shí)顯示 ?? ??? ?//根據(jù)RadioGroup取出數(shù)據(jù),沒(méi)有選中返回-1 ?? ??? ?String str = ""; ?? ??? ?int buttonId = group1.getCheckedRadioButtonId(); ?? ??? ?if(buttonId != -1){ ?? ??? ??? ?RadioButton radio = (RadioButton) findViewById(buttonId); ?? ??? ??? ?str = "你的性別是" + radio.getText().toString();?? ??? ??? ? ?? ??? ?}else{ ?? ??? ??? ?str = "你沒(méi)有選擇性別"; ?? ??? ?} ?? ??? ?buttonId = group2.getCheckedRadioButtonId(); ?? ??? ?if(buttonId != -1){ ?? ??? ??? ?RadioButton radio = (RadioButton) findViewById(buttonId); ?? ??? ??? ?str += ", ? 你吃飯了嗎?"+radio.getText().toString(); ?? ??? ?} ?? ??? ?Toast.makeText(this, str, Toast.LENGTH_LONG).show(); ?? ?} }
activity_check.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:id="@+id/LinearLayout1" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? tools:context="${relativePackage}.${activityClass}" > ? ? ? <TextView ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="選擇所學(xué)課程:" /> ? ? ? <CheckBox ? ? ? ? android:id="@+id/check1" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="HTML" /> ? ? <CheckBox ? ? ? ? android:id="@+id/check2" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="C" /> ? ? <CheckBox ? ? ? ? android:id="@+id/check3" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="php" /> ? ?? ? ? <CheckBox ? ? ? ? android:id="@+id/check4" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="java" /> ? ? ? <Button ? ? ? ? android:id="@+id/button1" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="提交" /> ? </LinearLayout>
CheckActivity.java
package com.example.radioandcheckdemo; ? import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Toast; ? public class CheckActivity extends Activity { ?? ? ?? ?private CheckBox check1; ?? ?private CheckBox check2; ?? ?private CheckBox check3; ?? ?private CheckBox check4; ?? ?private Button button1; ?? ? ?? ?private OnCheckedChangeListener listenter = new OnCheckedChangeListener() { ?? ??? ? ?? ??? ?@Override ?? ??? ?public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ?? ??? ??? ?//選中多選框 ?? ??? ??? ?CheckBox check = (CheckBox)buttonView; ?? ??? ??? ?//取出當(dāng)前勾選值 ?? ??? ??? ?String str = check.getText().toString(); ?? ??? ??? ?//判斷是否勾選狀態(tài) ?? ??? ??? ?if(isChecked){ ?? ??? ??? ??? ?str = "你學(xué)了"+str; ?? ??? ??? ?}else{ ?? ??? ??? ??? ?str = "你沒(méi)學(xué)"+str; ?? ??? ??? ?} ?? ??? ??? ?Toast.makeText(CheckActivity.this, str, Toast.LENGTH_LONG).show(); ?? ??? ?} ?? ?}; ? ?? ?@Override ?? ?protected void onCreate(Bundle savedInstanceState) { ?? ??? ?super.onCreate(savedInstanceState); ?? ??? ?setContentView(R.layout.activity_check); ?? ??? ? ?? ??? ?check1 = (CheckBox) findViewById(R.id.check1); ?? ??? ?check2 = (CheckBox) findViewById(R.id.check2); ?? ??? ?check3 = (CheckBox) findViewById(R.id.check3); ?? ??? ?check4 = (CheckBox) findViewById(R.id.check4); ?? ??? ?button1 = (Button) findViewById(R.id.button1); ?? ??? ? ?? ??? ?//多選框點(diǎn)擊事件 ?? ??? ?/*check1.setOnCheckedChangeListener(listenter); ?? ??? ?check2.setOnCheckedChangeListener(listenter); ?? ??? ?check3.setOnCheckedChangeListener(listenter); ?? ??? ?check4.setOnCheckedChangeListener(listenter);*/ ?? ??? ? ?? ??? ?//提交按鈕點(diǎn)擊事件 ?? ??? ?button1.setOnClickListener(new OnClickListener() { ?? ??? ??? ? ?? ??? ??? ?@Override ?? ??? ??? ?public void onClick(View v) { ?? ??? ??? ??? ?String str = "我學(xué)過(guò)了"; ?? ??? ??? ??? ?boolean f = false; ?? ??? ??? ??? ?if(check1.isChecked()){ ?? ??? ??? ??? ??? ?str += check1.getText()+","; ?? ??? ??? ??? ??? ?f = true; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?if(check2.isChecked()){ ?? ??? ??? ??? ??? ?str += check2.getText()+","; ?? ??? ??? ??? ??? ?f = true; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?if(check3.isChecked()){ ?? ??? ??? ??? ??? ?str += check3.getText()+","; ?? ??? ??? ??? ??? ?f = true; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?if(check4.isChecked()){ ?? ??? ??? ??? ??? ?str += check4.getText()+","; ?? ??? ??? ??? ??? ?f = true; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?if(f){ ?? ??? ??? ??? ??? ?str = str.substring(0, str.length()-1); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?Toast.makeText(CheckActivity.this, str, Toast.LENGTH_LONG).show(); ?? ??? ??? ?} ?? ??? ?}); ?? ?} }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)自動(dòng)變換大小的ViewPager
- android?viewpager實(shí)現(xiàn)輪播效果
- Android使用ViewPager實(shí)現(xiàn)翻頁(yè)效果
- Android自定義View實(shí)現(xiàn)遙控器按鈕
- Android實(shí)現(xiàn)單選按鈕
- Android 中使用RadioGroup和Fragment實(shí)現(xiàn)底部導(dǎo)航欄的功能
- Android基礎(chǔ)控件RadioGroup使用方法詳解
- Android RadioGroup多行顯示效果 解決單選問(wèn)題
- Kotlin RadioGroup與ViewPager實(shí)現(xiàn)底層分頁(yè)按鈕方法
相關(guān)文章
Android事件分發(fā)機(jī)制(下) View的事件處理
這篇文章主要介紹了Android事件分發(fā)機(jī)制下篇, View的事件處理的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Android獲得內(nèi)/外置存儲(chǔ)卡路徑的方法
我們知道Android上一般都有外置的存儲(chǔ)卡,內(nèi)置存儲(chǔ)卡路徑大家都知道怎么獲得的。那么如何獲取外置存儲(chǔ)卡的位置呢?下面小編通過(guò)本文給大家分享下2017-01-01Android開發(fā)教程之如何屏蔽View的重復(fù)點(diǎn)擊
這篇文章主要給大家介紹了關(guān)于Android開發(fā)教程之如何屏蔽View的重復(fù)點(diǎn)擊的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Android?Flutter使用本地?cái)?shù)據(jù)庫(kù)編寫備忘錄應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android?Flutter如何使用本地?cái)?shù)據(jù)庫(kù)實(shí)現(xiàn)編寫簡(jiǎn)單的備忘錄應(yīng)用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-03-03ProtoBuf動(dòng)態(tài)拆分Gradle?Module解析
這篇文章主要為大家介紹了ProtoBuf動(dòng)態(tài)拆分Gradle?Module解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Android PopupWindow全屏詳細(xì)介紹及實(shí)例代碼
這篇文章主要介紹了 Android PopupWindow全屏詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12Android中PopupWindow彈出式窗口使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android中PopupWindow彈出式窗口的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android訪問(wèn)php取回json數(shù)據(jù)實(shí)例
Android訪問(wèn)php取回json數(shù)據(jù),實(shí)現(xiàn)代碼如下,遇到訪問(wèn)網(wǎng)絡(luò)的權(quán)限不足在AndroidManifest.xml中,需要進(jìn)行如下配置2013-06-06