setOnCheckedChangeListener监听check事件
clearCheck选择状态消除
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout 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="228px" android:layout_height="49px" android:text="@string/str_radio_question1" android:textSize="30sp" android:layout_x="37px" android:layout_y="3px" /> <!--建立一個RadioGroup --> <RadioGroup android:id="@+id/myRadioGroup" android:layout_width="137px" android:layout_height="216px" android:orientation="vertical" android:layout_x="3px" android:layout_y="54px" > <!--第一個RadioButton --> <RadioButton android:id="@+id/myRadioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tr_radio_op1" /> <!--第二個RadioButton --> <RadioButton android:id="@+id/myRadioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tr_radio_op2" /> <Button android:onClick="bt" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </RadioGroup> </AbsoluteLayout>
package com.example.radiogroup; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView; public class MainActivity extends ActionBarActivity { public TextView mTextView1; public RadioGroup mRadioGroup1; public RadioButton mRadio1,mRadio2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView1 = (TextView) findViewById(R.id.myTextView); mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup); mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1); mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2); mRadioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(mRadio1.getId() == checkedId){ mTextView1.setText("nan"); }else{ mTextView1.setText("nv"); } } }); } public void bt(View view){ //将选择状态消除掉 mRadioGroup1.clearCheck(); } }
时间: 2024-10-11 01:51:07