如何在onClick方法上添加警告对话框?

时间:2014-06-26 08:49:17

标签: android mobile onclick alertdialog

我想添加一个警告对话框,以便在执行请求时具有加载状态。是否可以做类似的事情:

xButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        xButton.setVisibility(View.INVISIBLE);

        AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
        alertDialog.setMessage(Please wait);
        alertDialog.show();

        // do some work here
        alertdialog.dismiss

    }
});

        }
        return view;     

提前谢谢

3 个答案:

答案 0 :(得分:1)

如果您没有任何特定理由使用“警报对话”,则可以使用“进度对话框”。

@Override
public void onClick(View v) {
    xButton.setVisibility(View.INVISIBLE);

            new AsyncTask<Void, Void, Void>(){

                    ProgressDialog mProgressDialog;
                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        mProgressDialog = new ProgressDialog(<YourActivityClassName>.this);
                        mProgressDialog.setMessage("message");
                        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                        mProgressDialog.setCancelable(false);
                        mProgressDialog.show();
                    }

                    @Override
                    protected Void doInBackground(Void... params) {
                        // Do your work
                        return null;
                    }

                    protected void onPostExecute(Void result) {
                        super.onPostExecute(result);
                        mProgressDialog.dismiss();
                    };

                }.execute();


}

答案 1 :(得分:0)

使用一个函数显示警告并从onclick函数调用它。

public void showAlert() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                mcontext);

        // set title
        alertDialogBuilder.setTitle("Headding");

        // set dialog message
        alertDialogBuilder.setMessage("message")
                .setCancelable(false)
                .setPositiveButton(mcontext.getResources().getString(R.string.Ok), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
//                      
                    }
                });
        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();
        // show it
        alertDialog.show();
    }

答案 2 :(得分:0)

嗨阿德里安给出一个简单的代码希望这会有所帮助。美好的一天...

AlertDialog.Builder alertDialog=new AlertDialog.Builder(context);
                        alertDialog.setTitle("title");
                        alertDialog.setIcon(R.drawable.alert_image);
                        alertDialog.setMessage("alert message").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                current_activity.this.finish();
                                Intent inlog=new Intent(current_activity.this,target_activity.class);
                                startActivity(inlog);
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) 
                            {
                                dialog.cancel();
                            }
                        });

                        AlertDialog alertDialog2=alertDialog.create();
                        alertDialog2.show();