網站首頁 編程語言 正文
本文實例為大家分享了Android單選多選按鈕使用的具體代碼,供大家參考,具體內容如下
一、單選按鈕
單選按鈕類:RadioButton
android:checked="true"設置默認選中
單選按鈕控件通常與RadioGroup搭配使用。?
- RadioGroup是LinearLayout的子類,用于將多個單選按鈕組合為一組。?
- 同一按鈕組內的單選按鈕只能有一個被選中。
二、多選按鈕
用法基本與Button相同
CheckBox對象.isChecked()方法可以用來判斷復選按鈕是否選中?
效果圖(單選多選寫在一個項目里邊,用了一個頁面跳轉):
項目目錄:
多選按鈕,兩種形式
代碼:
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:
?? ??? ??? ?//跳轉頁面
?? ??? ??? ?intent.setClass(MainActivity.this, RadioActivity.class);
?? ??? ??? ?startActivity(intent);
?? ??? ??? ?break;
?? ??? ?case R.id.button2:
?? ??? ??? ?//跳轉頁面
?? ??? ??? ?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"設置默認選中 ? ? ?--> ? ? <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();
?? ??? ?
?? ??? ?//兩組數據同時顯示
?? ??? ?//根據RadioGroup取出數據,沒有選中返回-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="選擇所學課程:" /> ? ? ? <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;
?? ??? ??? ?//取出當前勾選值
?? ??? ??? ?String str = check.getText().toString();
?? ??? ??? ?//判斷是否勾選狀態
?? ??? ??? ?if(isChecked){
?? ??? ??? ??? ?str = "你學了"+str;
?? ??? ??? ?}else{
?? ??? ??? ??? ?str = "你沒學"+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 = "我學過了";
?? ??? ??? ??? ?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();
?? ??? ??? ?}
?? ??? ?});
?? ?}
}
原文鏈接:https://blog.csdn.net/qq_40205116/article/details/88796041
相關推薦
- 2022-12-31 C++?Boost?Uuid超詳細講解_C 語言
- 2022-08-29 React合成事件原理解析_React
- 2022-12-11 詳解Android?GLide圖片加載常用幾種方法_Android
- 2022-03-01 格式化日期‘年月日-時分秒’方法
- 2022-08-16 PostgreSQL怎么創建分區表詳解_PostgreSQL
- 2022-07-29 Python控制線程和函數超時處理_python
- 2023-10-14 uniapp在Android 10對公共目錄的非媒體文件讀取上傳失敗問題
- 2024-02-28 UNI-APP,設置某個頁面橫屏后,恢復豎屏,返回再次進入其他頁面時,頁面內容放大錯亂
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支