RadioButton實現多選一,radiobutton多選

來源:互聯網
上載者:User

RadioButton實現多選一,radiobutton多選
RadioButton實現多選一

一、簡介

 

二、RadioButton實現多選一方法

1、將多個RadioButton放在一個RadioGroup裡面

 1     <RadioGroup 2         android:id="@+id/radioGroup1" 3         android:layout_width="match_parent" 4         android:layout_height="wrap_content" > 5  6         <RadioButton 7             android:layout_width="wrap_content" 8             android:layout_height="wrap_content" 9             android:text="男"10             android:textColor="#FFFFFF" />11 12         <RadioButton13             android:layout_width="wrap_content"14             android:layout_height="wrap_content"15             android:text="女"16             android:textColor="#FFFFFF" />17     </RadioGroup>

 

2、在RadioGroup裡面取出每個RadioButton

 1 public void onClick(View v) { 2                 // TODO Auto-generated method stub 3                 int len = radioGroup1.getChildCount(); 4                 for (int i = 0; i < len; i++) { 5                     RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);11                 }12             }

 

3、檢查每個RadioButton是否被選取

1 if (radio.isChecked()) {

4 break;5 }

 

4、取出被選取的那個RadioButton裡面的值

1 Toast.makeText(Activity01.this, radio.getText(),2                                 Toast.LENGTH_LONG).show();

 

 

三、代碼執行個體

 

代碼:

fry.Activity01

 1 package fry; 2  3 import com.example.RadioButtonDemo1.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.TextView;13 import android.widget.Toast;14 15 public class Activity01 extends Activity {16     private Button btn_chooseGender;17     private RadioGroup radioGroup1;18     private TextView tv_answer;19 20     @Override21     protected void onCreate(Bundle savedInstanceState) {22         // TODO Auto-generated method stub23         super.onCreate(savedInstanceState);24         setContentView(R.layout.activity01);25 26         btn_chooseGender = (Button) findViewById(R.id.btn_chooseGender);27         radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);28         tv_answer = (TextView) findViewById(R.id.tv_answer);29         /*30          * RadioButton實現多選一方法31          * 1、將多個RadioButton放在一個RadioGroup裡面32          * 2、在RadioGroup裡面取出每個RadioButton 33          * 3、檢查每個RadioButton是否被選取34          * 4、取出被選取的那個RadioButton裡面的值35          */36         btn_chooseGender.setOnClickListener(new OnClickListener() {37 38             @Override39             public void onClick(View v) {40                 // TODO Auto-generated method stub41                 int len = radioGroup1.getChildCount();42                 for (int i = 0; i < len; i++) {43                     RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);44                     if (radio.isChecked()) {45                         Toast.makeText(Activity01.this, radio.getText(),46                                 Toast.LENGTH_LONG).show();47                         break;48                     }49                 }50             }51         });52     }53 }

/RadioButtonDemo1/res/layout/activity01.xml

 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:background="@android:color/black" 6     android:orientation="vertical" > 7  8     <TextView 9         android:layout_width="match_parent"10         android:layout_height="wrap_content"11         android:text="性別"12         android:textAppearance="?android:attr/textAppearanceLarge"13         android:layout_gravity="center_horizontal"14         android:textColor="#FFFFFF" />15 16     <RadioGroup17         android:id="@+id/radioGroup1"18         android:layout_width="match_parent"19         android:layout_height="wrap_content" >20 21         <RadioButton22             android:layout_width="wrap_content"23             android:layout_height="wrap_content"24             android:text="男"25             android:textColor="#FFFFFF" />26 27         <RadioButton28             android:layout_width="wrap_content"29             android:layout_height="wrap_content"30             android:text="女"31             android:textColor="#FFFFFF" />32     </RadioGroup>33 34     <Button 35         android:id="@+id/btn_chooseGender"36         android:layout_width="match_parent"37         android:layout_height="wrap_content"38         android:text="選擇性別"39         android:textColor="#FFFFFF" />40         />41         42     <TextView43         android:id="@+id/tv_answer"44         android:layout_width="match_parent"45         android:layout_height="wrap_content"46         android:text=""47         android:textAppearance="?android:attr/textAppearanceLarge"48         android:layout_gravity="center_horizontal"49         android:textColor="#FFFFFF" />50 </LinearLayout>

 

四、收穫

1、

android:textColor="#FFFFFF"

設定顏色,直接用#FFFFFF

2、

android:layout_gravity="center_horizontal"

文字置中顯示

3、

RadioButton在RadioGroup裡面實現多選一

4、

android:background="@android:color/black"

設定黑色,系統內建顏色

5、

int len = radioGroup1.getChildCount();

RadioGroup擷取孩子數量

6、

RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);

RadioGroup擷取孩子

7、

if (radio.isChecked())

判斷RadioButton是否被選取

8、

Toast.makeText(Activity01.this, radio.getText(),Toast.LENGTH_LONG).show();

多士

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.