Android 安卓:如何等待点击警报对话框?
我准备了一个带有“是/否”按钮的警报对话框的异步任务。我需要返回1表示是,返回0表示否。 我不完全理解这个任务。要增加单击的等待时间,需要做什么?当前状态不等待 代码类别:Android 安卓:如何等待点击警报对话框?,android,android-asynctask,dialog,return,android-alertdialog,Android,Android Asynctask,Dialog,Return,Android Alertdialog,我准备了一个带有“是/否”按钮的警报对话框的异步任务。我需要返回1表示是,返回0表示否。 我不完全理解这个任务。要增加单击的等待时间,需要做什么?当前状态不等待 代码类别: 我不明白你的问题。但通常异步任务看起来是这样的。在这里,我添加了回调接口以返回结果。如果你有时间试试这个。请 public class AsyncGet extends AsyncTask<Void, Void, String> { public interface AsyncGetResponse {
我不明白你的问题。但通常异步任务看起来是这样的。在这里,我添加了回调接口以返回结果。如果你有时间试试这个。请
public class AsyncGet extends AsyncTask<Void, Void, String> {
public interface AsyncGetResponse {
void processFinish(String result);
}
private AsyncGetResponse asyncGetResponse = null;
public AsyncGet(AsyncGetResponse asyncGetResponse) {
this.asyncGetResponse = asyncGetResponse;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// show progress dialog. If you need
}
@Override
protected String doInBackground(Void... voids) {
String result = null;
// do your background work here. Like API calls.
return result;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// update progress percentage. If you need.
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// hide progress dialog. If showing.
// pass result value.
if (asyncGetResponse != null) {
asyncGetResponse.processFinish(result);
}
}
}
快乐编码…解决了
此解决方案正在等待单击事件并返回值
private boolean resultValue;
public boolean getDialogValueBack(Context context)
{
final Handler handler = new Handler()
{
@Override
public void handleMessage(Message mesg)
{
throw new RuntimeException();
}
};
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setPositiveButton("Return True", new
DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
resultValue = true;
handler.sendMessage(handler.obtainMessage());
}
});
alert.setNegativeButton("Return False", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
resultValue = false;
handler.sendMessage(handler.obtainMessage());
}
});
alert.show();
try{ Looper.loop(); }
catch(RuntimeException e){}
return resultValue;
}
public class AsyncGet extends AsyncTask<Void, Void, String> {
public interface AsyncGetResponse {
void processFinish(String result);
}
private AsyncGetResponse asyncGetResponse = null;
public AsyncGet(AsyncGetResponse asyncGetResponse) {
this.asyncGetResponse = asyncGetResponse;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// show progress dialog. If you need
}
@Override
protected String doInBackground(Void... voids) {
String result = null;
// do your background work here. Like API calls.
return result;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// update progress percentage. If you need.
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// hide progress dialog. If showing.
// pass result value.
if (asyncGetResponse != null) {
asyncGetResponse.processFinish(result);
}
}
}
new AsyncGet(new AsyncGet.AsyncGetResponse() {
@Override
public void processFinish(String result) {
// process result from Async task.
}
}).execute();
private boolean resultValue;
public boolean getDialogValueBack(Context context)
{
final Handler handler = new Handler()
{
@Override
public void handleMessage(Message mesg)
{
throw new RuntimeException();
}
};
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setPositiveButton("Return True", new
DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
resultValue = true;
handler.sendMessage(handler.obtainMessage());
}
});
alert.setNegativeButton("Return False", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
resultValue = false;
handler.sendMessage(handler.obtainMessage());
}
});
alert.show();
try{ Looper.loop(); }
catch(RuntimeException e){}
return resultValue;
}