溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中獲取單選與復(fù)選框的值

發(fā)布時(shí)間:2021-04-14 17:41:31 來(lái)源:億速云 閱讀:587 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)怎么在Android中獲取單選與復(fù)選框的值,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/root"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TableRow>
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="性別"/>
    <!--定義一組單選按鈕-->
    <RadioGroup
      android:id="@+id/rg"
      android:orientation="horizontal"
      android:layout_gravity="center_horizontal">
      <!--定義兩個(gè)單選按鈕-->
      <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/male"
        android:text="男"
        android:checked="false"/>
      <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/female"
        android:text="女"
        android:checked="false"/>
    </RadioGroup>
  </TableRow>
  <TableRow>
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="喜歡的顏色"/>
    <!--定義一個(gè)垂直線性布局-->
    <LinearLayout
      android:layout_gravity="center_horizontal"
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      <!--定義三個(gè)復(fù)選框-->
      <CheckBox
        android:id="@+id/color_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="紅色"/>
      <CheckBox
        android:id="@+id/color_blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="藍(lán)色"/>
      <CheckBox
        android:id="@+id/color_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="綠色"/>
    </LinearLayout>
  </TableRow>
  <TextView
    android:id="@+id/show_sex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  <Button
    android:id="@+id/show"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="顯示復(fù)選框內(nèi)容"
    android:textSize="20pt"/>
  <TextView
    android:id="@+id/show_color"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</TableLayout>

Java代碼:

public class Home extends AppCompatActivity {
  RadioGroup radioGroup01 ;
  TextView textView01 ;
  TextView textView02 ;
  Button button01 ;
  CheckBox checkBox01 ;
  CheckBox checkBox02 ;
  CheckBox checkBox03 ;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);//顯示manLayout
    //連接組建
    radioGroup01 = (RadioGroup) findViewById(R.id.rg);
    textView01 = (TextView) findViewById(R.id.show_sex);
    textView02 = (TextView) findViewById(R.id.show_color);
    checkBox01 = (CheckBox) findViewById(R.id.color_red);
    checkBox02 = (CheckBox) findViewById(R.id.color_blue);
    checkBox03 = (CheckBox) findViewById(R.id.color_green);
    button01 = (Button) findViewById(R.id.show);
    //添加監(jiān)聽(tīng)事件
    radioGroup01.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        //根據(jù)用戶勾選信息改變tip字符串的值
        String tip = checkedId == R.id.male ?
        "您的性別為男" : "您的性別為n女" ;
        //修改show組件文本
        textView01.setText(tip);
      }
    });
    //輸出按鈕監(jiān)聽(tīng)事件
    button01.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        textView02.setText("喜歡的顏色: \n");
        //篩選復(fù)選框信息
        StringBuffer stringBuffer01 = new StringBuffer();
        stringBuffer01.append(textView02.getText().toString());
        if (checkBox01.isChecked()) {
          stringBuffer01.append("紅色\n");
        }
        if (checkBox02.isChecked()) {
          stringBuffer01.append("藍(lán)色\n");
        }
        if (checkBox03.isChecked()) {
          stringBuffer01.append("綠色");
        }
        textView02.setText(stringBuffer01.toString());
      }
    });
  }
}

關(guān)于怎么在Android中獲取單選與復(fù)選框的值就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI