国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

安卓控件 - 單選按鈕和復選框

這篇具有很好參考價值的文章主要介紹了安卓控件 - 單選按鈕和復選框。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。


一、介紹

安卓應用中,常常需要用戶從若干選項中進行選擇,有時要求只能選擇一個,那么就要使用單選按鈕(RadioButton),有時要求用戶可以選擇多個,那么就要使用復選框(CheckBox)

二、常用屬性

1、單選按鈕

  • 常用屬性
屬性 含義
orientation vertical (或 horizontal),決定單選按鈕是垂直排列還是水平排列
layout_width 寬度(單位:dp)
layout_height 高度(單位:dp)
  • 設置監(jiān)聽器
屬性 含義
setOnCheckedChangeListener 監(jiān)聽單選按鈕選中狀態(tài)的變化
setOnClickListener 監(jiān)聽單選按鈕組是否被單擊了
isChecked() true 或false,顯示單選按鈕的選中狀態(tài)
setChecked() 參數(shù)是true或false,用來設置單選按鈕的選中狀態(tài)
  • 設置事件監(jiān)聽器
屬性 含義
setOnCheckedChangeListener 監(jiān)聽單選按鈕選中狀態(tài)的變化
setOnClickListener 監(jiān)聽單選按鈕是否被單擊了

2、復選框

  • 常用屬性
屬性 含義
isChecked() true 或false,顯示復選框的選中狀態(tài)
setChecked() 參數(shù)是true或false,用來設置復選框的選中狀態(tài)
  • 設置事件監(jiān)聽器
屬性 含義
setOnCheckedChangeListener 監(jiān)聽復選框選中狀態(tài)的變化
setOnClickListener -+監(jiān)聽復選框是否被單擊了

三、案例

1 、示例

android 選擇框,android

2、步驟

(1)新建項目

  • 基于Empty Activity創(chuàng)建安卓應用SetBasicInformation
    android 選擇框,androidandroid 選擇框,android
    android 選擇框,android

(2)制備背景素材

  • 拷貝到 drawable 目錄下
    android 選擇框,android

(3)字符串資源文件 - strings.xml

android 選擇框,android

<resources>
    <string name="app_name">設置基本信息</string>
    <string name="set_information">設置基本信息</string>
    <string name="name">姓名:</string>
    <string name="input_name">請輸入姓名</string>
    <string name="gender">性別:</string>
    <string name="male"></string>
    <string name="female"></string>
    <string name="hobby">愛好:</string>
    <string name="music">音樂</string>
    <string name="read">閱讀</string>
    <string name="food">美食</string>
    <string name="ok">確定</string>
    <string name="clear">清除</string>
    <string name="exit">退出</string>
</resources>

(4)主布局資源文件 - activity_main.xml

android 選擇框,android

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="30dp">

    <TextView
        android:id="@+id/tv_setInformation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="30dp"
        android:text="@string/set_information"
        android:textColor="#0000ff"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name"
            android:textColor="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/input_name"
            android:textSize="20sp"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gender"
            android:textColor="#000000"
            android:textSize="20sp" />

        <RadioGroup
            android:id="@+id/rg_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/male"
                android:textSize="20sp" />

            <RadioButton
                android:id="@+id/rb_female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/female"
                android:textSize="20sp" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_hobby"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hobby"
            android:textColor="#000000"
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_music"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/music"
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/read"
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_food"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/food"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <Button
            android:id="@+id/btnOk"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doOK"
            android:layout_marginRight="10dp"
            android:text="@string/ok"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doClear"
            android:layout_marginRight="10dp"
            android:text="@string/clear"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn_exit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doExit"
            android:text="@string/exit"
            android:textSize="20sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="20sp"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:background="#dddddd"/>
</LinearLayout>

(5)主界面類MainActivity

android 選擇框,android

  • 定義常量與變量
    android 選擇框,android
  • 通過資源標識符獲取控件實例
    android 選擇框,android
  • 給按鈕組注冊單擊事件監(jiān)聽器
    android 選擇框,android
  • 確定按鈕單擊事件處理方法
    android 選擇框,android
  • 清除按鈕單擊事件處理方法
    android 選擇框,android
  • 推出按鈕單擊事件處理方法
    android 選擇框,android
package net.hxl.set_basic_information;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    private EditText etName;//姓名編輯框
    private RadioGroup rgGender;//性別單選按鈕
    private RadioButton rbMale;//男性單選按鈕
    private RadioButton rbFemale;//女性單選按鈕
    private CheckBox cbMusic;//音樂復選框
    private CheckBox cbRead;//閱讀復選框
    private CheckBox cbFood;//美食復選框
    private TextView tvResult;//結果標簽

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 通過資源標識符獲取控件實例
        etName = findViewById(R.id.et_name);
        rgGender = findViewById(R.id.rg_gender);
        rbMale = findViewById(R.id.rb_male);
        rbFemale = findViewById(R.id.rb_female);
        cbMusic = findViewById(R.id.cb_music);
        cbRead = findViewById(R.id.cb_read);
        cbFood = findViewById(R.id.cb_food);
        tvResult = findViewById(R.id.tv_result);

        // 給按鈕組注冊單擊事件監(jiān)聽器
        rgGender.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (rgGender.getCheckedRadioButtonId()){
                    case R.id.rb_male:// 選中男性單選按鈕
                        Toast.makeText(MainActivity.this, "你選擇", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rb_female:// 選中女性單選按鈕
                        Toast.makeText(MainActivity.this, "你選擇", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

    }
    /**
    * 確定按鈕單擊事件處理方法
    *
    * @param view
    */
    public void doOK(View view){
        //獲取姓名
        String name = etName.getText().toString().trim();

        // 獲取性別
        String gender = "";
        // 判斷用戶選中那個按鈕
        switch (rgGender.getCheckedRadioButtonId()){
            case R.id.rb_male:// 選中男性單選按鈕
                gender = rbMale.getText().toString();
                break;
            case R.id.rb_female:// 選中女性單選按鈕
                gender = rbFemale.getText().toString();
                break;
        }
        // 獲取愛好(字符串拼接)
        StringBuilder builder = new StringBuilder();//字符串生成器
        // 判斷用戶是否選中音樂復選框
        if(cbMusic.isChecked()){
            builder.append(cbMusic.getText().toString() + " ");

        }
        // 判斷用戶是否選中了閱讀復選框
        if(cbRead.isChecked()){
            builder.append(cbRead.getText().toString() + " ");

        }
        // 判斷用戶是否選中了美食復選框
         if(cbFood.isChecked()){
             builder.append(cbFood.getText().toString() + " ");
         }
        String hobbies = builder.toString().trim();// 去除字符串前后的空格

        // 顯示基本信息
        String result = "姓名:" + name + "\n"
                + "性別:" + gender + "\n"
                + "愛好:" + hobbies;
        tvResult.setText(result);
    }

    /**
     * 清除按鈕單擊事件處理方法
     *
     * @param view
     */
    public void doClear(View view){
        // 清空編輯框
        etName.setText("");
        // 清空復選框
        rbMale.setChecked(true);
        cbMusic.setChecked(false);
        cbRead.setChecked(false);
        cbFood.setChecked(false);
        // 清空編輯框
        tvResult.setText("");
    }

    /**
     * 推出按鈕單擊事件處理方法
     *
     * @param view
     */
    public void doExit(View view){
        finish();//關閉當前窗口
    }
}

android 選擇框,android文章來源地址http://www.zghlxwxcb.cn/news/detail-736927.html

到了這里,關于安卓控件 - 單選按鈕和復選框的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • Selenium常用操作之單選復選框、下拉列表、鍵盤、截屏、斷言、(顯式隱式)等待

    Selenium常用操作之單選復選框、下拉列表、鍵盤、截屏、斷言、(顯式隱式)等待

    目錄 1. 窗口最大化 2.單選框操作 3. 復選框操作 4. 下拉列表 5. selenium 三種等待 6. 鍵盤操作 7.截屏 8.斷言 9. Selenium操作JS彈窗控件 10.鼠標懸停與釋放 (1) 強制等待 ?? 常用于調試 (2) 隱式等待 ??簡介:?? ?? a 、 driver.implicitly_wait(10),隱式等待設置了一個時間,在一段時

    2024年02月03日
    瀏覽(23)
  • element-ui的el-table表格復選框只能單選,不可多選

    element-ui的el-table表格復選框只能單選,不可多選

    element的el-table表格復選框只能選中一條,選擇下一條,上一條去掉勾選。使用文檔中select方法,el-table綁定一個ref。具體代碼實現(xiàn)如下 toggleRowSelection:用于多選表格,切換某一行的選中狀態(tài),如果使用了第二個參數(shù),則是設置這一行選中與否(selected 為 true 則選中)?

    2024年02月11日
    瀏覽(29)
  • uniapp-checkbox-group,checkbox復選框多選,單選拿到一個value數(shù)組值
  • Pygame 基礎教程13: 使用 精靈(Sprite) 實現(xiàn) 按鈕、開關按鈕 和 復選框

    Pygame 基礎教程13: 使用 精靈(Sprite) 實現(xiàn) 按鈕、開關按鈕 和 復選框

    原文鏈接:https://xiets.blog.csdn.net/article/details/131395471 版權聲明:原創(chuàng)文章禁止轉載 專欄目錄:Pygame 專欄(總目錄) Pygame 中并沒有按鈕、開關按鈕、復選框 等基礎交互組件,但實際游戲開發(fā)中又經(jīng)常用到。例如用于點擊按鈕開始游戲的「開始按鈕」,用于開啟/關閉背景音樂

    2024年02月04日
    瀏覽(28)
  • C# 讀取帶CheckBox復選框控件的表格-并集成到Windows Service里面

    C# 讀取帶CheckBox復選框控件的表格-并集成到Windows Service里面

    最近的項目要求讀取xls文件內(nèi)的單元格,并且單元格旁邊會有復選框標識類型。 搜了下只有java的POI有例子,NOPI看項目文檔好像是沒有實現(xiàn)讀取控件的功能。 java實現(xiàn) POI? POI如何解析出excel 中復選框是否被選中 https://blog.csdn.net/qq_29832217/article/details/104413475? C#導出 Excel 時,

    2024年02月14日
    瀏覽(79)
  • Android開發(fā)-Android常用組件-Checkbox復選框

    Android開發(fā)-Android常用組件-Checkbox復選框

    2.CheckBox (復選框) 如題,復選框,即可以同時選中多個選項,至于獲得選中的值,同樣有兩種方式: 1.為每個CheckBox添加事件:setOnCheckedChangeListener 2.弄一個按鈕,在點擊后,對每個checkbox進行判斷:isChecked(); check_box.xml: ? ?MainActivity.java: 進行運行測試: 選中香蕉??/蘋果??

    2024年02月07日
    瀏覽(29)
  • Android:設置復選框 CheckBox 的顏色

    Android:設置復選框 CheckBox 的顏色

    Android:設置復選框 CheckBox 的顏色 meta charset=\\\"utf-8\\\" 如何設置復選框在不同狀態(tài)的顏色? 默認樣式 image 預期樣式 image meta charset=\\\"utf-8\\\" 先定義Checkbox的style,在values文件下的styles.xml文件中加入: colorControlNormal是未選中的顏色 ,colorControlActivated表示選中時的顏色, 自己在values下的

    2024年02月06日
    瀏覽(23)
  • 合宙Air724UG LuatOS-Air LVGL API控件--復選框 (Checkbox)

    合宙Air724UG LuatOS-Air LVGL API控件--復選框 (Checkbox)

    復選框 (Checkbox) 復選框主要是讓用戶進行一些內(nèi)容選擇,或者同意用戶協(xié)議。 示例代碼 – 復選框回調函數(shù) function event_handler(obj, event) if event == lvgl.EVENT_VALUE_CHANGED then print(“State”, lvgl.checkbox_is_checked(obj)) end end – 創(chuàng)建復選框 cb = lvgl.checkbox_create(lvgl.scr_act(), nil) – 設置標簽

    2024年02月11日
    瀏覽(23)
  • Element-Puls Form表單內(nèi)嵌套el-table表格,根據(jù)表格復選框多選或單選動態(tài)設置行的驗證規(guī)則

    Element-Puls Form表單內(nèi)嵌套el-table表格,根據(jù)表格復選框多選或單選動態(tài)設置行的驗證規(guī)則

    根據(jù) Table 表格內(nèi)的復選框來控制當前選中行是否添加必填校驗規(guī)則 我們需要設置一個 flag 來標識已勾選的行,el-table渲染數(shù)據(jù)結構是數(shù)組對象形式,我們可以在每個對象中手動加如一個標識,例如默認:selected : false,如你的源數(shù)據(jù)中已有類似key,則可用它作于唯一標識 htm

    2024年02月02日
    瀏覽(34)
  • Layui禁止表格部分復選框,layui禁止表格自帶第一列復選框,layui禁止表格部分復選框,layui獲取表格復選框選中數(shù)據(jù)

    Layui禁止表格部分復選框,layui禁止表格自帶第一列復選框,layui禁止表格部分復選框,layui獲取表格復選框選中數(shù)據(jù)

    禁止某些行可以勾選操作,及選中后的操作和行操作 執(zhí)行一個table示例 cols 的第一列就是需要重寫的復選框 其中templet: \\\"#toolCheck\\\"的標簽塊 監(jiān)聽全選重寫 獲取復選框選中的值,tempTableList當前列表返回的數(shù)組 以上即可! 監(jiān)聽行復選框點擊(如果沒有重寫復選框就可以用) 標簽

    2024年02月09日
    瀏覽(40)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包