我有三个单选按钮..现在当我想在单选按钮之间切换时我正在使用警报对话框..现在单击是,选择了另一个单选按钮但是在否它无法转到上一个单选按钮..
这是我的代码:
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 的条件不起作用...请帮助
答案 0 :(得分:0)
试试这个:
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()
答案 1 :(得分:0)
只需从OnCheckedCHangeListener
中删除此行lastCheckedid = radioGroup.getCheckedRadioButtonId();
此行将新检查的值保存到lastCheckedId中,然后创建AlertDialog。现在,在switch中,没有任何事情会发生,因为lastCheckedId具有当前检查的Id。只需删除该行,然后重试
答案 2 :(得分:0)
您的问题很难获得已选中按钮的先前值并返回检查该状态。首先,你的编码有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();
}