在广播组中选择单选按钮必须启用textview&选择单选按钮为必须禁用android中的textview

时间:2014-02-04 09:03:43

标签: android radio-group

在广播组中选择单选按钮为必须启用textview&选择单选按钮必须禁用android中的textview。

任何人都可以解释我该怎么做?

布局代码:

<RadioGroup
    android:id="@+id/rg5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rg5r1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="NO" />

    <RadioButton
        android:id="@+id/rg5r2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="YES" />

</RadioGroup>

<TextView
    android:id="@+id/q6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="If &apos;Yes&apos; please list allergies"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/wight" />

2 个答案:

答案 0 :(得分:6)

class MainActivity extends Activity {
RadioGroup rad;
TextView q6;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    q6=(TextView) findViewById(R.id.q6);
    rad=(RadioGroup) findViewById(R.id.rg5);
    rad.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int id=rad.getCheckedRadioButtonId();
              View radioButton = rad.findViewById(id);
              if(radioButton.getId()==R.id.rg5r1)
              {
                  q6.setEnabled(false);
              }
              else
              {
                  q6.setEnabled(true);
              }
        }
    }); 
}

答案 1 :(得分:1)

请按照以下内容更新您的活动:

public class MainActivity extends Activity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R.id.q6);
        textView.setVisibility(View.GONE);
    }

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
        case R.id.rg5r1:
            if (checked)
                textView.setVisibility(View.GONE);
            break;
        case R.id.rg5r2:
            if (checked)
                textView.setVisibility(View.VISIBLE);
            break;
        }
    }

}

和activity_main.xml像这样:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RadioGroup
        android:id="@+id/rg5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rg5r1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="NO" 
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:id="@+id/rg5r2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="YES"
            android:onClick="onRadioButtonClicked" />
    </RadioGroup>

    <TextView
        android:id="@+id/q6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="If &apos;Yes&apos; please list allergies"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF" />

</RelativeLayout>