如何检查选中的单选按钮?
How to check the selected radiobutton?
我尝试检查选中的单选按钮,
找到了一点细节,贴不上去
现在我有一个(View View)错误。
程序不知道选择了哪一个,
我找不到解决方案。
我还能写些什么?
请帮忙。
谢谢:蚂蚁Z
picture from the problem
我的代码:
package myapplication.Buttons;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText textView = findViewById(R.id.text);
Button button = findViewById(R.id.button);
Button btn1 = (Button)findViewById(R.id.button);
Button btn2 = (Button)findViewById(R.id.exit);
editText = findViewById(R.id.text);
RadioGroup radioGroup = findViewById(R.id.radioGroup1);
editText.addTextChangedListener(new TextWatcher() {
//do something }
});
public void getSelectedRadioButton(View View ) { <<< fail here :(View View )
final RadioGroup radGroup = findViewById(R.id.radiogroup1);
int radioID = radGroup.getCheckedRadioButtonId();
RadioButton singleButton = findViewById(radioID);
}
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup radioGroup = findViewById(R.id.radiogroup1);
<<< if i push the btn1 the program don't know the radioID>>>
if (radioGroup.getCheckedRadioButtonId() == R.id.radioButton_green) {
//do something
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
} );
}}
你应该做两件事来解决这个问题。
进行区分大小写的更正,将View View
更正为View view
将函数移到 onCreate 函数下方,MainActivity 内部。
你实际上不需要 1&2 来得到你想要的。
您可以复制粘贴此代码并开始工作。
代码:
package myapplication.Buttons;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.uploadedlobster.PwdHash.R;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText textView = findViewById(R.id.text);
Button button = findViewById(R.id.button);
Button btn1 = (Button)findViewById(R.id.button);
Button btn2 = (Button)findViewById(R.id.exit);
editText = findViewById(R.id.text);
RadioGroup radioGroup = findViewById(R.id.radioGroup1);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (radioGroup.getCheckedRadioButtonId() == R.id.radioButton_green) {
//do something
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
} );
}
}
编码愉快! :)
我尝试检查选中的单选按钮,
找到了一点细节,贴不上去
现在我有一个(View View)错误。
程序不知道选择了哪一个,
我找不到解决方案。
我还能写些什么?
请帮忙。
谢谢:蚂蚁Z picture from the problem
我的代码:
package myapplication.Buttons;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText textView = findViewById(R.id.text);
Button button = findViewById(R.id.button);
Button btn1 = (Button)findViewById(R.id.button);
Button btn2 = (Button)findViewById(R.id.exit);
editText = findViewById(R.id.text);
RadioGroup radioGroup = findViewById(R.id.radioGroup1);
editText.addTextChangedListener(new TextWatcher() {
//do something }
});
public void getSelectedRadioButton(View View ) { <<< fail here :(View View )
final RadioGroup radGroup = findViewById(R.id.radiogroup1);
int radioID = radGroup.getCheckedRadioButtonId();
RadioButton singleButton = findViewById(radioID);
}
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup radioGroup = findViewById(R.id.radiogroup1);
<<< if i push the btn1 the program don't know the radioID>>>
if (radioGroup.getCheckedRadioButtonId() == R.id.radioButton_green) {
//do something
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
} );
}}
你应该做两件事来解决这个问题。
进行区分大小写的更正,将
View View
更正为View view
将函数移到 onCreate 函数下方,MainActivity 内部。
你实际上不需要 1&2 来得到你想要的。
您可以复制粘贴此代码并开始工作。
代码:
package myapplication.Buttons;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.uploadedlobster.PwdHash.R;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText textView = findViewById(R.id.text);
Button button = findViewById(R.id.button);
Button btn1 = (Button)findViewById(R.id.button);
Button btn2 = (Button)findViewById(R.id.exit);
editText = findViewById(R.id.text);
RadioGroup radioGroup = findViewById(R.id.radioGroup1);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (radioGroup.getCheckedRadioButtonId() == R.id.radioButton_green) {
//do something
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
} );
}
}
编码愉快! :)