文字不会显示在文本框上(radiobutton)

时间:2014-05-01 13:22:58

标签: java android

当我点击一个单选按钮时,我希望它的值显示在文本框上。我不知道我的代码有什么问题,没有错误,只是当我勾选时它不会显示单选按钮

这是我的班级。

public class ConRadioSamp extends ActionBarActivity {


EditText txtans;

RadioButton RD1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_con_radio_samp);


}

public void ChkCommand(View v)
{
    txtans = (EditText)findViewById(R.id.txtcon1);

    if(v.getId()==R.id.chk1 && ((RadioButton)v).isChecked())
    {
        ((RadioButton) findViewById(R.id.radio2)).setChecked(false);
        ((RadioButton) findViewById(R.id.radio1)).setChecked(false);
        RD1 = (RadioButton) findViewById(R.id.radio0);


    }
    else if(v.getId()==R.id.chk2 && ((RadioButton)v).isChecked())
    {
        ((RadioButton) findViewById(R.id.radio0)).setChecked(false);
        ((RadioButton) findViewById(R.id.radio2)).setChecked(false);
        RD1 = (RadioButton) findViewById(R.id.radio1);

    }
    else if(v.getId()==R.id.chk3 && ((RadioButton)v).isChecked())
    {
        ((RadioButton) findViewById(R.id.radio0)).setChecked(false);
        ((RadioButton) findViewById(R.id.radio1)).setChecked(false);
        RD1 = (RadioButton) findViewById(R.id.radio2);

    }

    String value = RD1.getText().toString();
    txtans.setText(value);

}

public void CloseConF(View v)
{
    Intent intent = new Intent(this, SampComponents.class);
    startActivity(intent);
    finish();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.con_radio_samp, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_con_radio_samp,
                container, false);
        return rootView;
    }
}

}

2 个答案:

答案 0 :(得分:2)

您必须使用RadioGroup。为此,在xml文件中加一个RadiGroup并在其中添加RadioButton您想要的数量。

 RadioGroup rg;

之后在你的java文件中使用这种方式

 rg = (RadioGroup) findViewById(R.id.rgroup);

现在用你的ChkCommand()方法设置

 RadioButton rd1 = (RadioButton) findViewById(rg
                .getCheckedRadioButtonId());
 String radivalue = rd1.getText().toString();

现在将此文字设置为EditText

 txtans.setText(radivalue);

答案 1 :(得分:1)

尝试以下代码:

它是带有无线电组的xml。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        style="?background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >



        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="10dp" >

            <RadioButton
                android:id="@+id/btnContact"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:checked="true" />

            <View
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_weight="1" />

            <RadioButton
                android:id="@+id/btnDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
    />

            <View
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_weight="1" />

            <RadioButton
                android:id="@+id/btnShare"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
     />
        </RadioGroup>

    </LinearLayout>