Android之RadioGroup獲取選中值的方法?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
獲取選中的RadioButton兩種方式:
第一種方式
通過radioGroup.getCheckedRadioButtonId()來得到選中的RadioButton的ID,從而利用findviewbyid得到RadioButton進而獲取選中值
1.布局文件
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp">
android:id="@+id/safety_production"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:textColor="@color/font_2"
android:textSize="18sp"
android:padding="5dp"
android:tag="2"/>
android:id="@+id/emergency_drill"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:textColor="@color/font_2"
android:textSize="18sp"
android:padding="5dp"
android:tag="3"/>
android:id="@+id/other_services"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:textColor="@color/font_2"
android:textSize="18sp"
android:padding="5dp"
android:tag="4"/>
2.具體用法
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
selectRadioBtn();
}
});
private void selectRadioBtn(){
RadioButton rb = (RadioButton)ServiceRequestActivity.this.findViewById(radioGroup.getCheckedRadioButtonId());
text.setText(rb.getText);
}
第二種方式
需要利用一下三個方法
(1)radiogroup.getChildCount() 獲取radiogroup中子組件(radioButton)的數目
當前標題:Android之RadioGroup獲取選中值的方法-創新互聯
當前地址:http://m.kartarina.com/article6/ccgpig.html
成都網站建設公司_創新互聯,為您提供搜索引擎優化、網站內鏈、云服務器、軟件開發、企業建站、小程序開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯