从OnClick退出AlertDialog.Builder [英] Dismiss AlertDialog.Builder from OnClick

查看:130
本文介绍了从OnClick退出AlertDialog.Builder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使其弹出,以便为用户弹出一个对话框,该对话框的主体中有两个按钮,底部有一个取消按钮.当用户单击两个按钮之一时,对话框将消失,而单击取消"将仅从对话框中取消.取消部分工作正常,但我不知道如何手动关闭对话框.这是我的代码:

I'm trying to make it so that a dialog pops up for users which has two buttons in the body and a cancel button at the bottom. When a user clicks one of the two buttons the dialog will disappear, and hitting cancel will just cancel out of the dialog. The cancel part works fine, but I can't figure out how to dismiss the dialog manually. Here's my code:

public void onItemClick(AdapterView<?> parent, View view,
                    final int position, long id) {

                Context mContext = getApplicationContext();
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
                View layout = inflater.inflate(R.layout.config_dialog,
                        (ViewGroup) findViewById(R.id.config_dialog));

                Button connect = (Button) layout.findViewById(R.id.config_connect);
                Button delete = (Button) layout.findViewById(R.id.config_delete);

                alert = new AlertDialog.Builder(Configuration.this);
                alert.setTitle("Profile");

                connect.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        trace("Connect" + Integer.toString(position));
                        toast("Connected");
                        SharedPreferences app_preferences = 
                                PreferenceManager.getDefaultSharedPreferences(Configuration.this);
                        SharedPreferences.Editor editor = app_preferences.edit();
                        editor.putString("IP", fetch.get(position).IP);
                        editor.commit();
                        //Add dismiss here


                    }

                });

                delete.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {

                        trace("Delete");

                    }

                });


                // Set layout 
                alert.setView(layout);

                alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                    }
                });

                alert.show();

当我尝试添加alert.dismiss()时,Eclipse给了我一个错误. .dismiss()也不会显示在警报的自动完成列表中.

When I try to add the alert.dismiss(), Eclipse gives me an error. .dismiss() also doesn't show up in alert's autocomplete list.

推荐答案

AlertDialog.Builder最适合于小型简单对话框,而不是自定义对话框.

AlertDialog.Builder is best suited for small simple dialog boxes rather than custom dialogs.

处理自定义对话框的最干净方法是将AlertDialog子类作为上下文中的私有静态类(在本例中为您的活动).

The cleanest way to handle custom dialogs is to subclass AlertDialog as a private static class in your context (in this case your activity).

这是一个简化的示例:

public class AlertDialogTestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AlertDialog alert = new myCustomAlertDialog(this);
        alert.show();

    }

    private static class myCustomAlertDialog extends AlertDialog {

        protected myCustomAlertDialog(Context context) {
            super(context);

            setTitle("Profile");

            Button connect = new Button(getContext());
            setView(connect);
            connect.setText("Don't push me");
            connect.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // I want the dialog to close at this point
                    dismiss();
                }
            });
        }

    }
}

这篇关于从OnClick退出AlertDialog.Builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆