Java 获取按钮的字符串id

Java 获取按钮的字符串id,java,android,eclipse,radio-button,Java,Android,Eclipse,Radio Button,我的按钮有一个字符串id <RadioButton android:id="@+id/Dirty" android:text="Very Dirty" 要获取组件的名称,请使用getName()。我认为这将有助于?使用getName()获取组件的名称。我想这会有帮助吗?试试这个: int selectedRdBtnId = radioGroup.getCheckedRadioButtonId(); radioButton = (RadioButton) findViewById(s

我的按钮有一个字符串id

<RadioButton 
android:id="@+id/Dirty" 
android:text="Very Dirty" 

要获取组件的名称,请使用getName()。我认为这将有助于?

使用getName()获取组件的名称。我想这会有帮助吗?

试试这个:

int selectedRdBtnId = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedRdBtnId);
String rbText = radioButton.getText();
编辑:

为了获得id,我建议实现onCheckedChangeListener

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int radioButtonID) {
       //radioButtonID contains selected radio button's ID.

       case R.id.dirty:
           //do something
           break;
       case R.id.clean:
           //something else
           break;
       default:
           //default action
           break;
    }
});
希望这有帮助。

试试这个:

int selectedRdBtnId = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedRdBtnId);
String rbText = radioButton.getText();
编辑:

为了获得id,我建议实现onCheckedChangeListener

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int radioButtonID) {
       //radioButtonID contains selected radio button's ID.

       case R.id.dirty:
           //do something
           break;
       case R.id.clean:
           //something else
           break;
       default:
           //default action
           break;
    }
});
希望这有帮助。

试试这个 将标记设置为单选按钮,并在java类中获取该标记

Xml

Src

试试这个 将标记设置为单选按钮,并在java类中获取该标记

Xml

Src

使用方法:

使用方法:


解决方法是在单选按钮中添加一个与id同名的标签(除了id外):


因此,不便之处在于向单选按钮添加与id相同的标签。

解决方法是向单选按钮添加与id同名的标签(除id外):


因此,给您带来的不便是向单选按钮添加标签,这些标签与其id相同。

如果您想在按钮处使用字符串引用,您应该使用
Tag
而不是
id

,如果您想在按钮处使用字符串引用,你应该使用
标签
而不是
Id

我想你能找到答案

这个

getResources().getResourceName(int resid);

似乎对你有用。

我想你能找到答案

这个

getResources().getResourceName(int resid);

似乎对您很有用。

您能发布您的代码吗?RadioButton rb=(RadioButton)findViewById(selectedId);String sel=(String)rb.getText();你能发布你的代码吗?RadioButton rb=(RadioButton)findViewById(selectedId);String sel=(String)rb.getText();getText只返回按钮的显示名称,而不是按钮的id名称。问题是我知道选择了哪个单选按钮,我可以获得id,但id将始终返回一个整数。我需要给按钮的字符串名称作为id。getText只返回按钮的显示名称,而不是按钮的id名称。问题是我知道选择了哪个单选按钮,我可以获得id,但id将始终返回整数。我需要我给按钮的字符串名作为id。getText只返回按钮的显示名,而不是按钮的id名。getText只返回按钮的显示名,而不是按钮的id名。
<RadioButton
    android:id="@+id/Dirty"
    android:tag="Dirty"
    android:text="Very Dirty"
    />
RadioButton radioButton = (RadioButton)findViewById(R.id.Dirty);
String tag = (String) radioButton.getTag();
getResources().getResourceName(int resid);