![](/img/trans.png)
[英]Dynamically adding radio button list according to string values in Android Studio?
[英]Android Studio getting values from radio button, and using it elsewhere
我下面有代碼,使用單選按鈕並獲取值並將其作為函數的字符串返回。 希望我可以在主程序的其他地方使用它。 但是,事實並非如此。 它將允許我使用變量btn
,如果我通過將其聲明為final string []
來執行atl-enter
建議,則它將返回null。 大多數在線教程和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;
}
我怎樣才能正確地獲取和返回btn
值的函數listeneronbutton()
?
您不能擁有同時添加OnCheckedChangeListener
並獲取String
的方法(因為職責分離和一個方法只應運行一次,而另一個方法則應更頻繁地運行)。 像這樣,您可以將方法instanceRadioGroup()
添加到onCreate()
或onCreateView()
,然后使用getButtonText()
方法獲取當前值。
同樣,變量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);
}
});
}
像這樣更改您的方法:
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;
}
將String btn
聲明為字段。 因此,您可以訪問班級內部的任何地方。
public class Test{
String btn;
public String listeneronbutton(){
return btn;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.