Android單選多選按鈕的使用方法
本文實例為大家分享了Android單選多選按鈕使用的具體代碼,供大家參考,具體內(nèi)容如下
一、單選按鈕
單選按鈕類:RadioButton
android:checked="true"設(shè)置默認選中
單選按鈕控件通常與RadioGroup搭配使用。
- RadioGroup是LinearLayout的子類,用于將多個單選按鈕組合為一組。
- 同一按鈕組內(nèi)的單選按鈕只能有一個被選中。
二、多選按鈕
用法基本與Button相同
CheckBox對象.isChecked()方法可以用來判斷復(fù)選按鈕是否選中
效果圖(單選多選寫在一個項目里邊,用了一個頁面跳轉(zhuǎn)):
項目目錄:
多選按鈕,兩種形式
代碼:
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)頁面 ?? ??? ??? ?intent.setClass(MainActivity.this, RadioActivity.class); ?? ??? ??? ?startActivity(intent); ?? ??? ??? ?break; ?? ??? ?case R.id.button2: ?? ??? ??? ?//跳轉(zhuǎn)頁面 ?? ??? ??? ?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è)置默認選中 ? ? ?--> ? ? <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="沒吃"/> ? ? ? ?? ? ? </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, "沒吃", Toast.LENGTH_LONG).show(); ?? ??? ??? ?break; ?? ??? ?default: ?? ??? ??? ?break; ?? ??? ?}*/ ?? ??? ? ?? ??? ?//找到點擊的RadioButton ?? ??? ?//RadioButton radio = (RadioButton) findViewById(checkedId); ?? ??? ?//取出RadioButton中的值 ?? ??? ?//String str = radio.getText().toString(); ?? ??? ?//彈框顯示選中的值 ?? ??? ?//Toast.makeText(this, str, Toast.LENGTH_LONG).show(); ?? ??? ? ?? ??? ?//兩組數(shù)據(jù)同時顯示 ?? ??? ?//根據(jù)RadioGroup取出數(shù)據(jù),沒有選中返回-1 ?? ??? ?String str = ""; ?? ??? ?int buttonId = group1.getCheckedRadioButtonId(); ?? ??? ?if(buttonId != -1){ ?? ??? ??? ?RadioButton radio = (RadioButton) findViewById(buttonId); ?? ??? ??? ?str = "你的性別是" + radio.getText().toString();?? ??? ??? ? ?? ??? ?}else{ ?? ??? ??? ?str = "你沒有選擇性別"; ?? ??? ?} ?? ??? ?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 = "你沒學(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); ?? ??? ? ?? ??? ?//多選框點擊事件 ?? ??? ?/*check1.setOnCheckedChangeListener(listenter); ?? ??? ?check2.setOnCheckedChangeListener(listenter); ?? ??? ?check3.setOnCheckedChangeListener(listenter); ?? ??? ?check4.setOnCheckedChangeListener(listenter);*/ ?? ??? ? ?? ??? ?//提交按鈕點擊事件 ?? ??? ?button1.setOnClickListener(new OnClickListener() { ?? ??? ??? ? ?? ??? ??? ?@Override ?? ??? ??? ?public void onClick(View v) { ?? ??? ??? ??? ?String str = "我學(xué)過了"; ?? ??? ??? ??? ?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(); ?? ??? ??? ?} ?? ??? ?}); ?? ?} }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android事件分發(fā)機制(下) View的事件處理
這篇文章主要介紹了Android事件分發(fā)機制下篇, View的事件處理的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01Android中WebView加載網(wǎng)頁設(shè)置進度條
這篇文章主要為大家詳細介紹了Android中WebView加載網(wǎng)頁設(shè)置進度條的相關(guān)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04Android開發(fā)教程之如何屏蔽View的重復(fù)點擊
這篇文章主要給大家介紹了關(guān)于Android開發(fā)教程之如何屏蔽View的重復(fù)點擊的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Android?Flutter使用本地數(shù)據(jù)庫編寫備忘錄應(yīng)用
這篇文章主要為大家詳細介紹了Android?Flutter如何使用本地數(shù)據(jù)庫實現(xiàn)編寫簡單的備忘錄應(yīng)用,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-03-03ProtoBuf動態(tài)拆分Gradle?Module解析
這篇文章主要為大家介紹了ProtoBuf動態(tài)拆分Gradle?Module解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02Android PopupWindow全屏詳細介紹及實例代碼
這篇文章主要介紹了 Android PopupWindow全屏詳細介紹及實例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12Android中PopupWindow彈出式窗口使用方法詳解
這篇文章主要為大家詳細介紹了Android中PopupWindow彈出式窗口的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-09-09Android訪問php取回json數(shù)據(jù)實例
Android訪問php取回json數(shù)據(jù),實現(xiàn)代碼如下,遇到訪問網(wǎng)絡(luò)的權(quán)限不足在AndroidManifest.xml中,需要進行如下配置2013-06-06