Android 學習筆記---擷取RadioGroup的選定值,androidradiogroup

來源:互聯網
上載者:User

Android 學習筆記---擷取RadioGroup的選定值,androidradiogroup

1,擷取RadioGroup控制項: RadioGroup radioGroup = (RadioGroup)findViewById(R.id.myRadioGroup);

2,擷取RadioButton控制項; RadioButton radioButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());

3,擷取選中的radio的值: String text = radioButton.getText().toString();

4,為radioGroup添加監聽事件,用來監聽組件內部的事件響應: radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //在這個函數裡面用來改變選擇的radioButton的數值,以及與其值相關的 //任何操作,詳見下文 selectRadioBtn(); } });

5,在onCreat中需要初始化上面的四條資訊;

6,整體的使用範例: protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);         setContentView(R.layout.waterselect);                 getViews();         setListenerForView(); }

private void getViews(){   //擷取庫內上水的radio組件資訊、   radioGroup = (RadioGroup)findViewById(R.id.isWaterByContent);   }

private void setListenerForView(){ //選擇radio  selectRadioBtn();   //庫內上水的監聽事件  radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override  public void onCheckedChanged(RadioGroup group, int checkedId) { selectRadioBtn();  }  }); }

private void selectRadioBtn(){     radioButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());     waterIn = radioButton.getText().toString();    Log.i("radio", waterIn);     }

來自:http://blog.sina.com.cn/s/blog_9c5364110101c1bj.html

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin" >    <!-- 定義RadioGroup控制項 ,代表政治面貌選擇組 -->    <RadioGroup        android:id="@+id/radiogroup1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical" >        <!-- 定義RadioButton控制項 ,代表黨員選項 -->        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="黨員" />        <!-- 定義RadioButton控制項 ,代表群眾選項 -->        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="群眾" />        <!-- 定義RadioButton控制項 ,代表團員選項 -->        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="團員" />    </RadioGroup>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="結果"        android:id="@+id/yl1"></TextView>        <!--android:layout_gravity="center_horizontal" />--></LinearLayout>
package com.example.yanlei.yl2;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;//匯入必備的包import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.CompoundButton.OnCheckedChangeListener;public class MainActivity extends AppCompatActivity {    private RadioGroup radiogroup1;        //定義足球的複選框對象    private TextView yl1;            //定義結果文本便簽對象    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //調用父類的onCreate方法        //通過setContentView方法設定當前頁面的布局檔案為activity_main        setContentView(R.layout.activity_main);        findView();            //擷取頁面中的控制項        setListener();        //設定控制項的監聽器    }    private void setListener() {        // TODO Auto-generated method stub        //設定所有Radiogroup的狀態改變監聽器        radiogroup1.setOnCheckedChangeListener(mylistener);    }    RadioGroup.OnCheckedChangeListener mylistener=new RadioGroup.OnCheckedChangeListener()    {        @Override        public void onCheckedChanged(RadioGroup Group, int Checkid) {            // TODO Auto-generated method stub            //設定TextView的內容顯示CheckBox的選擇結果            setText();        }    };    private void findView() {        // TODO Auto-generated method stub        //通過findViewById得到對應的控制項對象        radiogroup1 = (RadioGroup)findViewById(R.id.radiogroup1);        yl1 = (TextView)findViewById(R.id.yl1);    }    private void setText(){        String str;        yl1.setText("");    //清空TextView的內容        RadioButton radioButton = (RadioButton)findViewById(radiogroup1.getCheckedRadioButtonId());        int id= radiogroup1.getCheckedRadioButtonId();        str=radioButton.getText().toString();        yl1.setText("選擇對象是:id="+id+",值:"+str);    }}

 

聯繫我們

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