欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android單選多選按鈕的使用方法

 更新時間:2022年05月17日 16:12:42   作者:小馬?同學(xué)  
這篇文章主要為大家詳細介紹了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的事件處理

    這篇文章主要介紹了Android事件分發(fā)機制下篇, View的事件處理的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android中WebView加載網(wǎng)頁設(shè)置進度條

    Android中WebView加載網(wǎng)頁設(shè)置進度條

    這篇文章主要為大家詳細介紹了Android中WebView加載網(wǎng)頁設(shè)置進度條的相關(guān)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Android獲得內(nèi)/外置存儲卡路徑的方法

    Android獲得內(nèi)/外置存儲卡路徑的方法

    我們知道Android上一般都有外置的存儲卡,內(nèi)置存儲卡路徑大家都知道怎么獲得的。那么如何獲取外置存儲卡的位置呢?下面小編通過本文給大家分享下
    2017-01-01
  • Android開發(fā)教程之如何屏蔽View的重復(fù)點擊

    Android開發(fā)教程之如何屏蔽View的重復(fù)點擊

    這篇文章主要給大家介紹了關(guān)于Android開發(fā)教程之如何屏蔽View的重復(fù)點擊的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Android?Flutter使用本地數(shù)據(jù)庫編寫備忘錄應(yīng)用

    Android?Flutter使用本地數(shù)據(jù)庫編寫備忘錄應(yīng)用

    這篇文章主要為大家詳細介紹了Android?Flutter如何使用本地數(shù)據(jù)庫實現(xiàn)編寫簡單的備忘錄應(yīng)用,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-03-03
  • ProtoBuf動態(tài)拆分Gradle?Module解析

    ProtoBuf動態(tài)拆分Gradle?Module解析

    這篇文章主要為大家介紹了ProtoBuf動態(tài)拆分Gradle?Module解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Android PopupWindow全屏詳細介紹及實例代碼

    Android PopupWindow全屏詳細介紹及實例代碼

    這篇文章主要介紹了 Android PopupWindow全屏詳細介紹及實例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • Android NDK開發(fā)入門

    Android NDK開發(fā)入門

    本文主要對NDK產(chǎn)生的背景、使用NDK原因、NDK簡介、NDK開發(fā)環(huán)境的搭建、如何運行NDK提供的事例demo等進行了詳細的介紹。具有很好的參考價值,需要的朋友一起來看下吧
    2016-12-12
  • Android中PopupWindow彈出式窗口使用方法詳解

    Android中PopupWindow彈出式窗口使用方法詳解

    這篇文章主要為大家詳細介紹了Android中PopupWindow彈出式窗口的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Android訪問php取回json數(shù)據(jù)實例

    Android訪問php取回json數(shù)據(jù)實例

    Android訪問php取回json數(shù)據(jù),實現(xiàn)代碼如下,遇到訪問網(wǎng)絡(luò)的權(quán)限不足在AndroidManifest.xml中,需要進行如下配置
    2013-06-06

最新評論