我试图从RadioButton获取由String of String创建的值
我使用OClickListener()从RadioButton获取值但错误显示
的" java.lang.ClassCastException:android.widget.Button无法强制转换为android.widget.RadioButton
"
这是我的代码,请检查
for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
radioGroup.addView(radioButtonView, p);
}
Button btnDisplay = (Button) findViewById(R.id.button1);
btnDisplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
String s = ((RadioButton) v ).getText().toString();
Toast.makeText(Radio_Button.this, "This is: " + s,Toast.LENGTH_LONG).show();
}
&#13;
和LogCat是
FATAL EXCEPTION: main
Process: com.example.radiobuttondynamic, PID: 4741
java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.RadioButton
at com.example.radiobuttondynamic.Radio_Button$1.onClick(Radio_Button.java:64)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
&#13;
请提出建议以纠正它
感谢 谢谢
答案 0 :(得分:2)
v
是Button
,您可以在此行中将其投放到RadioButton
:
String s = ((RadioButton) v ).getText().toString();
试试这个:
如果您以编程方式添加了radioButton
,则setID
可以radioButtons
:
for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
radioButtonView.setID(i);
radioGroup.addView(radioButtonView, p);
}
然后
String s = ((RadioButton) findViewById(yourRadioButtonID) ).getText().toString();
答案 1 :(得分:1)
为什么要尝试将按钮转换为单选按钮? 无论如何,如果你想获得所选单选按钮的文本,请执行以下操作
btnDisplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
RadioButton selectedButton= (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId()); //you will get the selected radiobutton here
String s=selectedButton..getText().toString();
Toast.makeText(Radio_Button.this, "This is: " + s,Toast.LENGTH_LONG).show();
}
答案 2 :(得分:0)
使用此:
String s = radioButtonView.getText().toString();
而不是:
String s = ((RadioButton) v ).getText().toString();
答案 3 :(得分:-1)
我正在编写onclick功能。
public void onClick(View v)
{
int selectedId = radioGroup.getCheckedRadioButtonId();
if(selectedId !=-1)
{
View radioButton = radioGroup.findViewById(selectedId );
int radioId = radioGroup.indexOfChild(radioButton);
RadioButton btn = (RadioButton) radioGroup.getChildAt(radioId);
String selection = (String) btn.getText();
Toast.makeText(Radio_Button.this, "This is: " + s,Toast.LENGTH_LONG).show();
}
}
试试这个,如果您有任何问题,请发表评论。