java文本不会显示在文本框(单选按钮)上
当我单击单选按钮时,我希望它的值显示在文本框中。我不知道我的代码有什么问题,没有错误,只是当我点击单选按钮时它不会显示
这是我的课
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;
}
}
}
# 1 楼答案
您必须使用
RadioGroup
。为此,在xml文件中取一个RadiGroup
,然后在其中添加RadioButton
您想要多少然后在java文件中使用这种方式
现在在你的
ChkCommand()
方法中,这样设置现在将此文本设置为
EditText
# 2 楼答案
请尝试以下代码:
这是您的xml与广播组