【问题标题】:How to get text of the checked id from radio group using view binding?如何使用视图绑定从无线电组中获取已检查 ID 的文本?
【发布时间】:2022-01-21 12:17:42
【问题描述】:

我已经通过this 找到了一个常见的答案,即

        int selectedId = radioGroup.getCheckedRadioButtonId();

        radioButton = (RadioButton) findViewById(selectedId);

        Toast.makeText(MyAndroidAppActivity.this,
            radioButton.getText(), Toast.LENGTH_SHORT).show();

但是如何使用视图绑定来获取文本呢?

【问题讨论】:

    标签: android android-studio radio-button radio-group android-viewbinding


    【解决方案1】:

    你可以这样做:

    编辑:

    int id = binding.radioGroup.getCheckedRadioButtonId();
    RadioButton radioButton = binding.getRoot().findViewById(id);
    //Then get the radio button's text
    Toast.makeText(MyAndroidAppActivity.this,
            radioButton.getText(), Toast.LENGTH_SHORT).show();
    

    【讨论】:

    • getViewById 不起作用(显示错误)
    • 改用 findViewById
    • 成功了,谢谢
    • 欢迎您的 Kushal Sharma
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多