如果通过按钮点击检查AppCompatRadioButton,我会尝试获取文本 这是布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="status"
android:textColor="#f2ff00" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButtonFull"
android:text="full"
android:textColor="#f2ff00"
app:buttonTint="#ffffff" />
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButtonEnd"
android:textColor="#f2ff00"
app:buttonTint="#28ff1c"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonSearch"
android:textColor="#ffffff"/>
</LinearLayout>
这是我的代码:
AppCompatRadioButton rb_full = (AppCompatRadioButton)view.findViewById(R.id.radioButtonFull);
rb_full.setText("This is full !");
AppCompatRadioButton rb_end = (AppCompatRadioButton)view.findViewById(R.id.radioButtonEnd);
rb_end.setText("This is end !");
String status;
if(rb_full.isChecked()){
status = "Full, please delete something";
}
if(rb_end.isChecked()){
status = "End, please select one of it";
}
bt_Search = (Button) view.findViewById(R.id.buttonSearch);
bt_Search.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplicationContext(), status, Toast.LENGTH_SHORT).show();
}
});
如果通过按钮点击检查AppCompatRadioButton,我会尝试获取字符串状态 但是当点击按钮时,Toast什么也没显示 如何解决?
答案 0 :(得分:0)
您需要实现OnClickListener来更改变量Status中的值。你可以用两种方式做到这一点:
1)在无线电组本身上实现OnCheckedChangeListener:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
if(checkedId == R.id.radioButtonFull)
status = "Full, please delete something";
else
status = "End, please select one of it";
}
});
2)第二种方法是为单选按钮实现OnClickListener,但这将是冗余代码。所以我建议你只使用第一种方法。
答案 1 :(得分:0)
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButtonFull"
android:text="full"
android:checked="true"
android:textColor="#f2ff00"
app:buttonTint="#ffffff" />
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButtonEnd"
android:textColor="#f2ff00"
app:buttonTint="#28ff1c"/>
</RadioGroup>
JAVA
final RadioGroup rg = (RadioGroup) findViewById(R.id.rg);
bt_Search.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
int selectedId = rg.getCheckedRadioButtonId();
if(selectedId == R.id.radioButtonFull)
status = "Full, please delete something";
else
status = "End, please select one of it";
Toast.makeText(getActivity().getApplicationContext(), status, Toast.LENGTH_SHORT).show();
}
});
答案 2 :(得分:0)
您可以通过这种方式点击按钮获取选定的单选按钮值,然后选中this
public class MyActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyActivity.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}