对话框

来源:互联网 发布:淘宝网店卖商品收费吗 编辑:程序博客网 时间:2024/05/21 18:36
        protected Dialog onCreateDialog(int id) {
            return new AlertDialog.Builder(this)
                .setTitle(R.string.rgb_summary)
                .setPositiveButton(R.string.rgb_yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            mView.mTimer.cancel();
                            Utils.writeResult("pass");
                            finish();
                        }
                    })
                .setNegativeButton(R.string.rgb_no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            mView.mTimer.cancel();
                            Utils.writeResult("fail");
                           dialog.dismiss();
                        }
                    }).create();

    }

AlertDialog.Builder ad = new AlertDialog.Builder(this);

        ad.setTitle(R.string.factory_title);
        ad.setMessage(R.string.factory_message);
        ad.setPositiveButton(R.string.factory_title,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int i) {
                        Intent intent = new Intent(mContext,
                                ChooseActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                        BaseApplication appState = (BaseApplication)BootActivity.this.getApplication();
                        appState.startService();
                        finish();
                    }
                });
        ad.setNegativeButton(R.string.project_title,
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(mContext, MainProject.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                       dialog.dismiss();
                    }
                });
        ad.show();


自定义对话框:

LayoutInflater inflater1 = getLayoutInflater();
 View view1 = inflater1.inflate(R.layout.userdefinedtoast,  (ViewGroup) findViewById(R.id.toast_layout));
        TextView txtView_Title1 = (TextView) view1.findViewById(R.id.txt_Title);
         txtView_Title1.setText("chongxinmingming");

    AlertDialog.Builder  builder = new AlertDialog.Builder(this);

    builder.setView(view1);
    AlertDialog dialog = builder.create();
    dialog.show();


注:默认情况下,在点击dialog以外的屏幕时,dialog会消失的解决办法

dialog.setCancelable(false);

原创粉丝点击