java - 文本不会显示在文本框(单选按钮)上

标签 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;
    }
}

}

最佳答案

您必须使用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);

关于java - 文本不会显示在文本框(单选按钮)上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23408578/

相关文章:

java - 是否可以中断 ExecutorService 的特定线程?

java - 如何从tomcat服务器获取存储的图像并显示它

java - 如何使用反射从外部 JAR 文件检索一些资源

java - 将文件从 URL 流式传输到文件而不将其存储在内存中

android - 如何将搜索栏分成几个部分

Android Camera Preview 在虚拟手机中不起作用

java - 应用程序在 Socket 连接时崩溃

java - 手动处理事务而不是JtaTransactionManager

android - 将 Android Studio 3.0 更新到 3.3.1 时出错

android - fragment 它传递数据但不更新列表适配器