我有一个显示AlertDialog
的方法。单击“是”按钮时,将关闭对话框并执行AsyncTask
。使用“取消”按钮,它就会解散。我从其他两种方法调用这个方法。单击一个按钮时会调用它,而当设备被摇动时会调用它。当我通过点击按钮调用它时,Alertdialog工作正常。但是在设备震动时,当我点击“是”按钮时,AlertDialog不会被忽略。在两种情况下都会执行Asynctask。
这是我的代码: -
@Override
public void onShake(float force)
{
// TODO Auto-generated method stub
showDialog(timeString, "Confirm Running late by 1 hour.");
}
rlSaveRunningLate.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(timeString == null || hourLimit ==10 && minLimit ==30)
{
timeString = hour+":"+mins+":"+today.second;
Toast.makeText(getActivity(), "Please select time.", Toast.LENGTH_LONG).show();
}
else
{
showDialog(timeString, "Confirm");
}
}
});
public void showDialog(String timeString, String title)
{
builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title);
builder.setMessage("Reaching at:"+" "+ timeString+"?");
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
dialog.cancel();
}
});
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
// Do nothing but close the dialog
dialog.dismiss();
dialog.cancel();
new AddTask().execute();
}
});
builder.show();
}
答案 0 :(得分:0)
试试这个
public void onClick(DialogInterface dialog, int which)
{
new AddTask().execute();
dialog.dismiss();
dialog.cancel();
}