如何在单选按钮 android 之间切换
how to switch between Radio Button android
我有三个单选按钮..现在,当我想在单选按钮之间切换时,我正在使用警报对话框..现在单击 YES,另一个单选按钮是选中但在 NO 上它无法转到上一个单选按钮..
这是我的代码:
RadioGroup radioGroup;
RadioButton pre, during, post;
String lastChecked = "pre"; //This is the Flag which i m using
radioGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lastCheckedid = radioGroup.getCheckedRadioButtonId();
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
lastCheckedid = radioGroup.getCheckedRadioButtonId();
if (pre.isChecked()) {
lastChecked = "pre";
getlastchecked();
} else if (during.isChecked()) {
lastChecked = "during";
getlastchecked();
} else if (post.isChecked()) {
lastChecked = "post";
getlastchecked();
}
}
});
private void getlastchecked() {
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(MainActivity.this);
alertDialog2.setTitle("Confirm Delete...");
alertDialog2.setMessage("Are you sure you want delete this post ?");
alertDialog2.setIcon(R.drawable.delete);
alertDialog2.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
imageView1.setImageDrawable(null);
textview_imagename1.setText("");
imageView2.setImageDrawable(null);
textview_imagename2.setText("");
imageView3.setImageDrawable(null);
textview_imagename3.setText("");
}
});
alertDialog2.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (lastCheckedid) {
case R.id.pre:
if (lastChecked.equals("during")){
during.setChecked(true);}
else if (lastCheck.equals("post")){
post.setChecked(true);}
break;
case R.id.during:
if (lastChecked.equals("pre")){
pre.setChecked(true);}
else if (lastCheck.equals("post")){
post.setChecked(true);}
break;
case R.id.post:
if (lastChecked.equals("during")){
during.setChecked(true);}
else if (lastCheck.equals("pre")){
pre.setChecked(true);}
break;
}
}
});
alertDialog2.show();
}
NO 的条件不工作...请帮助
试试这个:
RadioGroup registerGender = (RadioGroup)findViewById(R.id.regsiter_gender);
int selectedId = registerGender.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButtonGender = (RadioButton) findViewById(selectedId);
radioButtonGender.getText().toString().trim()
只需从 OnCheckedCHangeListener 中删除这一行
lastCheckedid = radioGroup.getCheckedRadioButtonId();
这一行会将新检查的值保存到 lastCheckedId 中,然后创建 AlertDialog。现在,在 switch 中,不会发生任何事情,因为 lastCheckedId 具有当前选中的 Id。只需删除该行并重试
您的问题很难获取选中按钮的先前值并返回检查到该状态。首先,您的编码具有 lastCheckedid,这根据使用名称是不正确的。因此,我已将其更改为 curCheckedid 并尝试在演示中实施您的问题的解决方案。我希望这会解决你的问题。试试下面的代码:-
在定义变量中
RadioGroup radioGroup;
RadioButton pre, during, post;
String lastChecked = "pre"; //This is the Flag which i m using
int curCheckedid;
boolean isClickSet = true;
之后是初始化侦听器:-
lastChecked = "pre";
curCheckedid = pre.getId();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
curCheckedid = radioGroup.getCheckedRadioButtonId();
if (pre.isChecked()) {
if(lastChecked != "pre" && isClickSet)
getlastchecked();
else {
isClickSet = true;
lastChecked = "pre";
}
} else if (during.isChecked()&& isClickSet) {
if(lastChecked != "during")
getlastchecked();
else {
isClickSet = true;
lastChecked = "during";
}
} else if (post.isChecked()&& isClickSet) {
if(lastChecked != "post")
getlastchecked();
else {
isClickSet = true;
lastChecked = "post";
}
}
}
});
和单独的 getlastchecked() 方法:-
private void getlastchecked() {
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(MainActivity.this);
alertDialog2.setTitle("Confirm Delete...");
alertDialog2.setMessage("Are you sure you want delete this post ?");
alertDialog2.setIcon(R.drawable.delete);
alertDialog2.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
imageView1.setImageDrawable(null);
textview_imagename1.setText("");
imageView2.setImageDrawable(null);
textview_imagename2.setText("");
imageView3.setImageDrawable(null);
textview_imagename3.setText("");
if (pre.isChecked())
lastChecked = "pre";
else if (during.isChecked())
lastChecked = "during";
else if (post.isChecked())
lastChecked = "post";
isClickSet = true;
}
});
alertDialog2.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (curCheckedid) {
case R.id.pre:
if (lastChecked.equals("during")){
isClickSet = false;
during.setChecked(true);
isClickSet = true;
lastChecked = "during";}
else if (lastChecked.equals("post")){
isClickSet = false;
post.setChecked(true);
isClickSet = true;
lastChecked = "post";}
break;
case R.id.during:
if (lastChecked.equals("pre")){
isClickSet = false;
pre.setChecked(true);
isClickSet = true;
lastChecked = "pre";}
else if (lastChecked.equals("post")){
isClickSet = false;
post.setChecked(true);
isClickSet = true;
lastChecked = "post";}
break;
case R.id.post:
if (lastChecked.equals("during")){
isClickSet = false;
during.setChecked(true);
isClickSet = true;
lastChecked = "during";}
else if (lastChecked.equals("pre")){
isClickSet = false;
pre.setChecked(true);
isClickSet = true;
lastChecked = "pre";}
break;
}
}
});
alertDialog2.show();
}
我有三个单选按钮..现在,当我想在单选按钮之间切换时,我正在使用警报对话框..现在单击 YES,另一个单选按钮是选中但在 NO 上它无法转到上一个单选按钮..
这是我的代码:
RadioGroup radioGroup;
RadioButton pre, during, post;
String lastChecked = "pre"; //This is the Flag which i m using
radioGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lastCheckedid = radioGroup.getCheckedRadioButtonId();
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
lastCheckedid = radioGroup.getCheckedRadioButtonId();
if (pre.isChecked()) {
lastChecked = "pre";
getlastchecked();
} else if (during.isChecked()) {
lastChecked = "during";
getlastchecked();
} else if (post.isChecked()) {
lastChecked = "post";
getlastchecked();
}
}
});
private void getlastchecked() {
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(MainActivity.this);
alertDialog2.setTitle("Confirm Delete...");
alertDialog2.setMessage("Are you sure you want delete this post ?");
alertDialog2.setIcon(R.drawable.delete);
alertDialog2.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
imageView1.setImageDrawable(null);
textview_imagename1.setText("");
imageView2.setImageDrawable(null);
textview_imagename2.setText("");
imageView3.setImageDrawable(null);
textview_imagename3.setText("");
}
});
alertDialog2.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (lastCheckedid) {
case R.id.pre:
if (lastChecked.equals("during")){
during.setChecked(true);}
else if (lastCheck.equals("post")){
post.setChecked(true);}
break;
case R.id.during:
if (lastChecked.equals("pre")){
pre.setChecked(true);}
else if (lastCheck.equals("post")){
post.setChecked(true);}
break;
case R.id.post:
if (lastChecked.equals("during")){
during.setChecked(true);}
else if (lastCheck.equals("pre")){
pre.setChecked(true);}
break;
}
}
});
alertDialog2.show();
}
NO 的条件不工作...请帮助
试试这个:
RadioGroup registerGender = (RadioGroup)findViewById(R.id.regsiter_gender);
int selectedId = registerGender.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButtonGender = (RadioButton) findViewById(selectedId);
radioButtonGender.getText().toString().trim()
只需从 OnCheckedCHangeListener 中删除这一行
lastCheckedid = radioGroup.getCheckedRadioButtonId();
这一行会将新检查的值保存到 lastCheckedId 中,然后创建 AlertDialog。现在,在 switch 中,不会发生任何事情,因为 lastCheckedId 具有当前选中的 Id。只需删除该行并重试
您的问题很难获取选中按钮的先前值并返回检查到该状态。首先,您的编码具有 lastCheckedid,这根据使用名称是不正确的。因此,我已将其更改为 curCheckedid 并尝试在演示中实施您的问题的解决方案。我希望这会解决你的问题。试试下面的代码:-
在定义变量中
RadioGroup radioGroup;
RadioButton pre, during, post;
String lastChecked = "pre"; //This is the Flag which i m using
int curCheckedid;
boolean isClickSet = true;
之后是初始化侦听器:-
lastChecked = "pre";
curCheckedid = pre.getId();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
curCheckedid = radioGroup.getCheckedRadioButtonId();
if (pre.isChecked()) {
if(lastChecked != "pre" && isClickSet)
getlastchecked();
else {
isClickSet = true;
lastChecked = "pre";
}
} else if (during.isChecked()&& isClickSet) {
if(lastChecked != "during")
getlastchecked();
else {
isClickSet = true;
lastChecked = "during";
}
} else if (post.isChecked()&& isClickSet) {
if(lastChecked != "post")
getlastchecked();
else {
isClickSet = true;
lastChecked = "post";
}
}
}
});
和单独的 getlastchecked() 方法:-
private void getlastchecked() {
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(MainActivity.this);
alertDialog2.setTitle("Confirm Delete...");
alertDialog2.setMessage("Are you sure you want delete this post ?");
alertDialog2.setIcon(R.drawable.delete);
alertDialog2.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
imageView1.setImageDrawable(null);
textview_imagename1.setText("");
imageView2.setImageDrawable(null);
textview_imagename2.setText("");
imageView3.setImageDrawable(null);
textview_imagename3.setText("");
if (pre.isChecked())
lastChecked = "pre";
else if (during.isChecked())
lastChecked = "during";
else if (post.isChecked())
lastChecked = "post";
isClickSet = true;
}
});
alertDialog2.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (curCheckedid) {
case R.id.pre:
if (lastChecked.equals("during")){
isClickSet = false;
during.setChecked(true);
isClickSet = true;
lastChecked = "during";}
else if (lastChecked.equals("post")){
isClickSet = false;
post.setChecked(true);
isClickSet = true;
lastChecked = "post";}
break;
case R.id.during:
if (lastChecked.equals("pre")){
isClickSet = false;
pre.setChecked(true);
isClickSet = true;
lastChecked = "pre";}
else if (lastChecked.equals("post")){
isClickSet = false;
post.setChecked(true);
isClickSet = true;
lastChecked = "post";}
break;
case R.id.post:
if (lastChecked.equals("during")){
isClickSet = false;
during.setChecked(true);
isClickSet = true;
lastChecked = "during";}
else if (lastChecked.equals("pre")){
isClickSet = false;
pre.setChecked(true);
isClickSet = true;
lastChecked = "pre";}
break;
}
}
});
alertDialog2.show();
}