关于获取RadioGroup里面的RadioButton的值,多行RadioGroup展示清除选中效果
2017-07-28 09:13
489 查看
代码部分:
public class MainActivity extends Activity {
public TextView mTextView1;
public RadioGroup mRadioGroup1,mRadioGroup2;
public RadioButton mRadio1, mRadio2,mRadio3,mRadio4;
public Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 取得 TextView、RadioGroup、RadioButton对象
mTextView1 = (TextView) findViewById(R.id.myTextView);
submit = (Button) findViewById(R.id.submit);
mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);
mRadioGroup2 = (RadioGroup) findViewById(R.id.myRadioGroup2);
mRadio3 = (RadioButton) findViewById(R.id.myRadioButton3);
mRadio4 = (RadioButton) findViewById(R.id.myRadioButton4);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(mTextView1.getText().toString().equals("")){
Toast toast = Toast.makeText(MainActivity.this, "mTextView1为空"+mTextView1.getText().toString(), Toast.LENGTH_SHORT);
toast.show();
}else{
Toast toast = Toast.makeText(MainActivity.this, "mTextView1选中的值为:"+mTextView1.getText().toString(), Toast.LENGTH_SHORT);
toast.show();
}
}
});
mRadioGroup1.setOnCheckedChangeListener(new OnMyManholeStateOneCheckedChangeListener());
mRadioGroup2.setOnCheckedChangeListener(new OnMyManholeStateTwoCheckedChangeListener());
}
private class OnMyManholeStateOneCheckedChangeListener implements
RadioGroup.OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int position) {
switch (position) {
case R.id.myRadioButton1:
if (mRadio1.isChecked())
mRadioGroup2.clearCheck();//清除RadioGroup2的选中状态
mTextView1.setText(mRadio1.getText());
break;
case R.id.myRadioButton2:
if (mRadio2.isChecked())
mRadioGroup2.clearCheck();
mTextView1.setText(mRadio2.getText());
break;
default:
break;
}
}
}
private class OnMyManholeStateTwoCheckedChangeListener implements
RadioGroup.OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int position) {
switch (position) {
case R.id.myRadioButton3:
if (mRadio3.isChecked())
mRadioGroup1.clearCheck();//清除RadioGroup1的选中状态
mTextView1.setText(mRadio3.getText());
break;
case R.id.myRadioButton4:
if (mRadio4.isChecked())
mRadioGroup1.clearCheck();
mTextView1.setText(mRadio4.getText());
break;
default:
break;
}
}
}
}
xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--第一个TextView -->
<TextView
android:id="@+id/myTextView"
android:layout_width="228dp"
android:layout_height="49dp"
android:textSize="18sp"
/>
<!--建立一个RadioGroup -->
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<!--第一个RadioButton -->
<RadioButton
android:id="@+id/myRadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton1"
/>
<!--第二个RadioButton -->
<RadioButton
android:id="@+id/myRadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton2"
/>
</RadioGroup>
<RadioGroup
android:id="@+id/myRadioGroup2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<!--第三个RadioButton -->
<RadioButton
android:id="@+id/myRadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton3"
/>
<!--第四个RadioButton -->
<RadioButton
android:id="@+id/myRadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton4"
/>
</RadioGroup>
<Button
android:id="@+id/submit"
android:text="提交"
android:layout_width="100dp"
android:layout_height="50dp"/>
</LinearLayout>
源代码地址:http://download.csdn.net/detail/wb935419471/9911736
public class MainActivity extends Activity {
public TextView mTextView1;
public RadioGroup mRadioGroup1,mRadioGroup2;
public RadioButton mRadio1, mRadio2,mRadio3,mRadio4;
public Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 取得 TextView、RadioGroup、RadioButton对象
mTextView1 = (TextView) findViewById(R.id.myTextView);
submit = (Button) findViewById(R.id.submit);
mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);
mRadioGroup2 = (RadioGroup) findViewById(R.id.myRadioGroup2);
mRadio3 = (RadioButton) findViewById(R.id.myRadioButton3);
mRadio4 = (RadioButton) findViewById(R.id.myRadioButton4);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(mTextView1.getText().toString().equals("")){
Toast toast = Toast.makeText(MainActivity.this, "mTextView1为空"+mTextView1.getText().toString(), Toast.LENGTH_SHORT);
toast.show();
}else{
Toast toast = Toast.makeText(MainActivity.this, "mTextView1选中的值为:"+mTextView1.getText().toString(), Toast.LENGTH_SHORT);
toast.show();
}
}
});
mRadioGroup1.setOnCheckedChangeListener(new OnMyManholeStateOneCheckedChangeListener());
mRadioGroup2.setOnCheckedChangeListener(new OnMyManholeStateTwoCheckedChangeListener());
}
private class OnMyManholeStateOneCheckedChangeListener implements
RadioGroup.OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int position) {
switch (position) {
case R.id.myRadioButton1:
if (mRadio1.isChecked())
mRadioGroup2.clearCheck();//清除RadioGroup2的选中状态
mTextView1.setText(mRadio1.getText());
break;
case R.id.myRadioButton2:
if (mRadio2.isChecked())
mRadioGroup2.clearCheck();
mTextView1.setText(mRadio2.getText());
break;
default:
break;
}
}
}
private class OnMyManholeStateTwoCheckedChangeListener implements
RadioGroup.OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int position) {
switch (position) {
case R.id.myRadioButton3:
if (mRadio3.isChecked())
mRadioGroup1.clearCheck();//清除RadioGroup1的选中状态
mTextView1.setText(mRadio3.getText());
break;
case R.id.myRadioButton4:
if (mRadio4.isChecked())
mRadioGroup1.clearCheck();
mTextView1.setText(mRadio4.getText());
break;
default:
break;
}
}
}
}
xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--第一个TextView -->
<TextView
android:id="@+id/myTextView"
android:layout_width="228dp"
android:layout_height="49dp"
android:textSize="18sp"
/>
<!--建立一个RadioGroup -->
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<!--第一个RadioButton -->
<RadioButton
android:id="@+id/myRadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton1"
/>
<!--第二个RadioButton -->
<RadioButton
android:id="@+id/myRadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton2"
/>
</RadioGroup>
<RadioGroup
android:id="@+id/myRadioGroup2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<!--第三个RadioButton -->
<RadioButton
android:id="@+id/myRadioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton3"
/>
<!--第四个RadioButton -->
<RadioButton
android:id="@+id/myRadioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton4"
/>
</RadioGroup>
<Button
android:id="@+id/submit"
android:text="提交"
android:layout_width="100dp"
android:layout_height="50dp"/>
</LinearLayout>
源代码地址:http://download.csdn.net/detail/wb935419471/9911736
相关文章推荐
- 关于获取RadioGroup里面的RadioButton的值,多行RadioGroup展示清除选中效果
- Android 如何获取RadioGroup选中RadioButton监听
- Android 如何获取RadioGroup选中RadioButton的内容
- 利用RadioGroup和RadioButton实现单选且选中的效果自定图片
- 自定义RadioGroup动态添加RadioButton,并获取选中radioButton的位置
- Android 如何获取RadioGroup选中RadioButton的内容
- Android 使用RadioGroup和RadioButton实现单选效果
- 关于Fragment的显示与隐藏结合RadioGroup的默认选中问题
- Android下利用RadioGroup和RadioButton实现Tabbar的效果
- RadioGroup出现多个RadioButton选中
- 利用Radiogroup Radiobutton 实现滑动效果菜单
- Android RadioGroup中设置默认选中RadioButton 后,选中两个的问题 解决方法
- 关于radio选中后,后台获取不了选中值的解决方法
- 关于RadioGroup某个item设置为check后,会一直选中的问题
- Android RadioGroup中的RadioButton无法选中问题的一种处理方式
- tabHost研究二——利用Radiobutton嵌套在RadioGroup实现TabHost效果
- Android布局中有两个RadioGroup的RadioButton选中问题
- 比如有10个radiobutton,他们的groupname相同,我想得到选中的radiobutto
- 可以解决RadioGroup中两个RadioButton选中以及RadioButton回行布局的RadioGroup
- RadioGroup动态添加RadioButton,RadioButton设置默认选中第一个,点击其他的RadioButton第一个不再选中,第一个不再选中