【问题标题】:Is there a way to show AlertDialog in AsyncTask commonly?有没有办法通常在 AsyncTask 中显示 AlertDialog ?
【发布时间】:2016-08-26 17:00:36
【问题描述】:

我想显示 AsyncTask 中其他类中的 AlertDialog。 示例>

public class WardListAsyncTask extends AsyncTask<HashMap<String, String>, String, Object> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Object doInBackground(HashMap<String, String>... params) {
      ...
    }

   @Override
    protected void onPostExecute(Object result) {
        ConfirmAlertDialog(ez_WardList.this,"HI?"); 
        //this method is in another Class & I want to use this method another Asynctask also..
    }

并且 ConfirmAlertDialog 是...

    Context g_ctx;
String g_content;
public void ez_ConfirmAlertDialog(Context ctx, String content) {
    this.g_ctx=ctx;
    this.g_content=content;

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT);
            builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                        }
                    });

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            alertDialog.getWindow().setLayout(displayWidth / 2, LayoutParams.WRAP_CONTENT);
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239));

        }
    });

}

我认为 g_ctx.class.runonuiThread ...但我不能调用 runonuithread ...

我该如何解决?

【问题讨论】:

标签: android android-asynctask android-alertdialog


【解决方案1】:

runOnUiThread 方法存在于 Activity 类中。所以应该将 Activity 传递给你的类。 示例:

public class MyClass{


public void ez_ConfirmAlertDialog(Activity activity, String content) {
    this.g_ctx=ctx;
    this.g_content=content;

    if(activity == null){
       return;
    }

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT);
            builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                        }
                    });

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            alertDialog.getWindow().setLayout(displayWidth / 2, LayoutParams.WRAP_CONTENT);
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239));

        }
    });

}

}

【讨论】:

  • 谢谢!!你节省我的时间!这对我很好。我只是想知道它是否永远不会调用有关活动的错误.. ?? cuiz 我正在将 Toast 更改为 AlertDialog... 这是自定义 Toast msg = message; runOnUiThread(new Runnable() { @ Override public void run() { View view = View.inflate(Form_Main.this, R.layout.common_toast, null); TextView tv = (TextView) view.findViewById(R.id.textViewToast ); tv.setText(msg); Toast t = new Toast(_context); t.setView(view); t.setDuration(Toast.LENGTH_LONG); t.setGravity(Gravity.CENTER, 0, 0); t.show ();});
  • @Adrian 在这种情况下,只有当活动为空时才会出现错误,因此您还应该在方法顶部检查活动是否为空。
  • 谢谢!!这对我来说是完美的。我解决了我的问题!
【解决方案2】:

您可以使用处理程序在主线程中调用它

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
             // Your code here
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    相关资源
    最近更新 更多