[英]Android Studio getting values from radio button, and using it elsewhere
I've got the code below, to use a radio button and get the value and return it as a string for a function. 我下面有代码,使用单选按钮并获取值并将其作为函数的字符串返回。 Hoping that I could use it elsewhere in the main program. 希望我可以在主程序的其他地方使用它。 However, it doesn't. 但是,事实并非如此。 it would allow me to use the variable btn
and if, I did the atl-enter
suggestion by declaring it to final string []
, it'll return null. 它将允许我使用变量btn
,如果我通过将其声明为final string []
来执行atl-enter
建议,则它将返回null。 Most online tutorial and stackoverflow previous question only includes toasting text from whichever button chosen, within onCheckedChanged
. 大多数在线教程和stackoverflow上一个问题仅包括在onCheckedChanged
从选择的任何按钮烘烤文本。
public String listeneronbutton() {
String btn;
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedID) {
int selectedId = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(checkedID);
Toast.makeText(getApplicationContext(), radioButton.getText(), Toast.LENGTH_SHORT).show();
btn = String.valueOf(radioButton.getText()); //(error here: variable 'btn' is accessed from within inner class, needs to be declared final)
}
});
return btn;
}
How do I get the function listeneronbutton()
properly being able to get and return btn
value? 我怎样才能正确地获取和返回btn
值的函数listeneronbutton()
?
you cannot have a method which adds the OnCheckedChangeListener
and gets the String
, at the same time (because separation of duties & the one method only should run once, the other method more often). 您不能拥有同时添加OnCheckedChangeListener
并获取String
的方法(因为职责分离和一个方法只应运行一次,而另一个方法则应更频繁地运行)。 alike this you can add method instanceRadioGroup()
to onCreate()
or onCreateView()
and then get the current value with method getButtonText()
. 像这样,您可以将方法instanceRadioGroup()
添加到onCreate()
或onCreateView()
,然后使用getButtonText()
方法获取当前值。
also, the variable int checkedId
is already being passed into the scope, so one can use that. 同样,变量int checkedId
已被传递到范围中,因此可以使用它。
/** the handle for the {@link RadioGroup} */
private RadioGroup mRadioGroup = null;
/** this field holds the button's text */
private String mButtonText = null;
/** the setter for the field */
protected void setButtonText(@Nullable String value) {
this.mButtonText = value;
}
/** the getter for the field */
protected String getButtonText() {
return this.mButtonText;
}
/** it sets mButtonText by checkedId */
protected void updateButtonText(int checkedId) {
if ((checkedId == -1)) {
this.setButtonText(null);
} else {
RadioButton radioButton = (RadioButton) this.mRadioGroup.findViewById(checkedId);
this.setButtonText(radioButton.getText());
}
}
/** this code should only run once, onCreate() or onCreateView() */
protected void instanceRadioGroup() {
/* setting the handle for the {@link RadioGroup} */
this.mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
/* update the field with the text of the default selection */
int checkedId = this.mRadioGroup.getCheckedRadioButtonId();
this.updateButtonText(checkedId);
/* and also add an onCheckedChange listener */
this.mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
updateButtonText(checkedId);
}
});
}
Change your method like this : 像这样更改您的方法:
public String listeneronbutton() {
String btn;
RadioGroup radioGroup =(RadioGroup)findViewById(R.id.radioGroup);
int selectedId = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(checkedID);
Toast.makeText(getApplicationContext(), radioButton.getText(), Toast.LENGTH_SHORT).show();
btn = String.valueOf(radioButton.getText());
return btn;
}
Declare String btn
as field. 将String btn
声明为字段。 Thus you can access anywhere inside class. 因此,您可以访问班级内部的任何地方。
public class Test{
String btn;
public String listeneronbutton(){
return btn;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.