我想创建一个关于非活动的警报对话框,并且知道除非创建警报活动,否则我无法在非活动中进行操作。
我实现了以下操作,但无法正常工作-如果我单击没有任何更改。
这是非活动中的警报类调用:
Class alertDeletion=AlertDeletion.class;
Intent myIntent = new Intent(mContext,alertDeletion );
mContext.startActivity(myIntent);
if (getFlag()){ // do some thing }
这是AlertDialog活动
public class AlertDeletion extends AppCompatActivity {
private static boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_deletion);
flag= confermation();
finish();
}
public boolean confermation(){
// boolean answer =false;
AlertDialog.Builder alert = new AlertDialog.Builder(
this);
alert.setTitle("Alert!!");
alert.setMessage("Are you sure to delete ");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setFlag(true);
dialog.dismiss();
}
});
alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setFlag(false);
dialog.dismiss();
}
});
AlertDialog builder = alert.create();
builder.show();
return getFlag();
}
private void setFlag(boolean t){
flag=t;
}
public static boolean getFlag(){
return flag;
}
}
答案 0 :(得分:0)
在if条件getFlag()
中使用alertDeletion.getFlag()
代替if (getFlag()){ // do some thing }