[英]Android Studio: Retrieve radio button id from activity_main.xml
我试图检索选中的单选按钮ID,以便随后可以进行切换,并根据用户检查的内容进行某些操作,然后按“完成调查”按钮。 但是,下面的方法检索一些奇数ID(不是我为XML文件中的单选按钮指定的一个ID。它为我提供了一个大名称,例如“ android.widget.RadioButton@5363daa0。”这是什么意思?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set up our textview
showChoiceTextView = (TextView)findViewById(R.id.showTextView);
radioGroup = (RadioGroup)findViewById(R.id.radioGroupID);
showButton = (Button)findViewById(R.id.showChoiceButton);
showButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedOption = radioGroup.getCheckedRadioButtonId();
radioChoiceButton = (RadioButton)findViewById(selectedOption);
String text = radioChoiceButton.toString();
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
}
});
要获取视图的文本,必须使用view.getText()方法。 请记住,radioGroup是一个对象,文本是它的值之一。
另外,要检索其ID,必须使用getId()方法。
您要检索的id是常量的名称,而getId()检索其值。 如果我说例如:
int id = 10;
然后10是它的值(使用getId()进行检索),而“ id”是它的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.