1、先上图
2、基础知识
2.1、RadioButton一般需要配合RadioGroup使用,否则Radio之间不能互斥;
2.2、RadioButton的事件一般绑定在RadioGroup接口中:RadioGroup.OnCheckedChangeListener,实现方法onCheckedChanged(RadioGroup group, @IdRes int checkedId);
2.3、RadioGroup通过getCheckedRadioButtonId()方法可获取当前被选中RadioButton的ID;
3、Demo说明
3.1、当选中RadioGroup中的某个RadioButton时,通过Log类将其id与文本输出;
3.2、当点击提交按钮时,修改TextView中的文字,与RadioButton中相关的文字关联起来;
4、关键代码
Activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | package com.yusian.demo; import android.support.annotation.IdRes; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity { private RadioGroup radioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { RadioButton radio = (RadioButton) findViewById(checkedId); Log.i("SALog", "id:"+checkedId+"-->"+radio.getText()); } }); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int id = radioGroup.getCheckedRadioButtonId(); if (id <= 0) return; RadioButton radio = (RadioButton)findViewById(id); TextView textView = (TextView)findViewById(R.id.textView); textView.setText("充值金额:"+radio.getText()); } }); } } |
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.yusian.demo.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="充值金额"/> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="50"/> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="100"/> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="200"/> </RadioGroup> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交"/> </LinearLayout> |