安卓開發_選項按鈕控制項(RadioButton)的簡單使用

來源:互聯網
上載者:User

標籤:

最近複習安卓基礎的時候發現沒有寫關於選項按鈕、複選按鈕的部落格,可能因為以前學習的時候感覺太簡單了就沒有寫,現在補上吧

 

當我們在各種用戶端註冊帳號的時候,會有幾項單選項,比如選擇您的性別。下面就是學習怎麼實現這種效果

 

一、安卓中,選項按鈕用RadioButton表示,因為RadioButton是Button的子類,所以可以使用Button的各種屬性

RadioButton一般是不單獨使用的需要結合RadioGroup控制項一起使用,將若干個RadioButton放在一個RadioGroup中,那麼將只能選擇RadioGroup中的某一個RadioButton

比如講 語文、數學,英語放在“你最喜歡的課程中” 那麼使用者將只能選擇一個課程

 

二、使用

首先看下布局檔案

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6      7       <TextView  8             android:layout_width="wrap_content" 9             android:layout_height="wrap_content"10             android:text="性別"11             android:textSize="18dp"/>12     <RadioGroup 13         android:layout_width="wrap_content"14         android:layout_height="wrap_content"15         android:id="@+id/id_radiogroup"16         android:orientation="horizontal"17         18         >19      20         <RadioButton 21             android:layout_width="wrap_content"22             android:layout_height="wrap_content"23             android:id="@+id/radio_1"24             android:checked="true"  //設定預設已被選擇中25             android:text="男"26             />27         <RadioButton 28             android:layout_width="wrap_content"29             android:layout_height="wrap_content"30             android:id="@+id/radio_2"31             android:text="女"32             />33 34     </RadioGroup>35     <Button 36         android:layout_width="wrap_content"37         android:layout_height="wrap_content"38         android:id="@+id/tijiao"39         android:text="提交"/>40 </LinearLayout>

可以看到兩個RadioButton被放到了一個RadioGroup中,這兩個RadioButton的值分別“男”,“女”

表示我們只能選擇其中的任意一項選項

:

 

下面再看JAVA檔案

 1 package base_ui; 2  3 import com.example.allcode.R; 4  5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button;10 import android.widget.RadioButton;11 import android.widget.RadioGroup;12 import android.widget.RadioGroup.OnCheckedChangeListener;13 import android.widget.Toast;14 15 public class Ui_RadioButton extends Activity{16     private RadioGroup radiogroup;17     private RadioButton radio_one;18     private RadioButton radio_two;19     private Button tijiao;20     String str; //存放點擊的按鈕的值21     @Override22     protected void onCreate(Bundle savedInstanceState) {23         // TODO Auto-generated method stub24         super.onCreate(savedInstanceState);25         setContentView(R.layout.base_ui_radiobutton);26         27         radiogroup = (RadioGroup) findViewById(R.id.id_radiogroup);28         radio_one = (RadioButton) findViewById(R.id.radio_1);29         radio_two = (RadioButton) findViewById(R.id.radio_2);30         tijiao = (Button) findViewById(R.id.tijiao);31         32         //改變選項按鈕組的值時的響應事件33         radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {34             35             @Override36             public void onCheckedChanged(RadioGroup group, int checkedId) {37                 // TODO Auto-generated method stub38                 RadioButton radio_button =  (RadioButton)findViewById(checkedId);39                 str = radio_button.getText().toString().trim();//擷取被選中的選項按鈕的值40                 Toast.makeText(Ui_RadioButton.this, "選擇選項按鈕的值為:"+str, 1).show();41             }42         });43         44         //單擊其他按鈕時的響應事件45         tijiao.setOnClickListener(new OnClickListener() {46             47             @Override48             public void onClick(View v) {49                 // TODO Auto-generated method stub50 51                 for(int i = 0;i<radiogroup.getChildCount();i++)  //迴圈按鈕組子按鈕的數量次52                 {53                     RadioButton r =  (RadioButton)radiogroup.getChildAt(i); //根據索引值擷取選項按鈕54                     if(r.isChecked()) //判斷按鈕是否被選中 55                     {56                         57                         str = r.getText().toString().trim();//擷取被選中的選項按鈕的值58                         Toast.makeText(Ui_RadioButton.this, "點擊提交按鈕時擷取的選項按鈕的值為:"+str, 1).show();59                         break;60                     }61                 }62             }63         });64     }65 66 }

注釋很明白了,很簡單的

:

安卓開發_選項按鈕控制項(RadioButton)的簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.