您的位置:首页 > 其它

关于获取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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: