Radio Button selection Changes Toast on Android
Posted
by Bub
on Stack Overflow
See other posts from Stack Overflow
or by Bub
Published on 2010-04-29T19:23:57Z
Indexed on
2010/04/29
19:27 UTC
Read the original article
Hit count: 236
android
|radiobutton
I was writing a simple test application. There are two radio buttons within the app. There id's are "radio_red" and "radio_blue". I wanted to create an onClickListener event that read the text associated to the button and then returned a basic "Right" or "Wrong" toast.
Here is a sample of the code:
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v){
RadioButton rb = (RadioButton) v;
String ans = rb.getText().toString();
String an1 = "";
if (ans.trim() == "Yes") {
ans = "That's Right.";
}
else if (ans.trim() == "No") {
ans = "thats wrong.";
}
else {
ans = "none.";
}
Toast.makeText(v.getContext(), ans , Toast.LENGTH_SHORT).show();
}
So far no joy. Here is my code snippet. I've checked my "main.xml" and the text associated to the buttons are referneced correctly. I added trim to make sure of that. However, all that is ever returned in the toast is "none." What am I missing? Thanks in advance for any help.
© Stack Overflow or respective owner