簡體   English   中英

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM