如果选择任何选项对话框preference不应关闭 [英] DialogPreference shouldn't close if no option selected

查看:124
本文介绍了如果选择任何选项对话框preference不应关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对话框preference,我想,以避免用户关闭它时,pressing确定,取消,等等。

I have a DialogPreference and I want to avoid the user from closing it when pressing "OK", "Cancel", etc.

我应该怎么办呢?

编辑:

我试图达到OK按钮创建对话框时禁用。但我不能使它:(

I tried to reach the OK button to disable when the dialog is created. But I couldn't make it :(

推荐答案

一个好办法可能是,你自己定义的按钮(OK和关闭),以创建一个自定义对话框。

A tweak could be to create a custom dialog where you define your own buttons (OK and Close).

public class YourClass implements OnClickListener {
    private Button DialogButton;
    private Dialog dialog;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.MainLayout);

        /* Your code... */

        DialogButton = (Button) findViewById(R.id.DialogButtonId);
        DialogButton.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.DialogButtonId:
            LayoutInflater inflater = LayoutInflater.from(YourClass.this);
            final View inflay = inflater.inflate(R.layout.DialogLayout, (ViewGroup) findViewById(R.id.RootIdOfDialogLayout));

            TextView YourTextView = (TextView) inflay.findViewById(R.id.TextViewId);

            Button cancel = (Button) inflay.findViewById(R.id.CancelButtonId);      
            cancel.setOnClickListener(YourClass.this);

            Button ok = (Button) inflay.findViewById(R.id.OkButtonId);      
            ok.setOnClickListener(YourClass.this);

            dialog = new Dialog(YourClass.this);
            dialog.setContentView(inflay);
            dialog.setTitle(getString(R.string.TitleStringId));
            dialog.show();
            break;
        case R.id.CancelButtonId:
            /* Checking if the user selected an option if true call dialog.dismiss() */
            break;
        case R.id.OkButtonId:
            /* Here handle your preferences (e.g. putString(String key, String value)) */
            /* Checking if the user selected an option if true call dialog.dismiss() */
            break;
        }
    }
}

查阅的http://developer.android.com/reference/android/content/Shared$p$pferences.Editor.html为了处理您的preference中的onClick。我没有测试code只是写它显示了您如何解决呢!

Check out http://developer.android.com/reference/android/content/SharedPreferences.Editor.html in order to handle your preference in onClick. I didn't test this code just wrote it to show you how you could solve it!

该对话框,直到调用dialog.dismiss(保持打开状态);在这种情况下,你必须创建你的下拉菜单,投票或者你想在你的布局文件来显示什么都。经过pressing确定或取消您应该检查用户是否做出了选择,并解析选择到preferences。 (检查以上链接)

The dialog stays open until you call dialog.dismiss();. In that case you'll have to create your drop-down-menu, polls or what ever you want to display in your layout file. After pressing ok or cancel you should check if the user made a choice, and parse that choice into your preferences. (check link above)

RGDS 莱恩

这篇关于如果选择任何选项对话框preference不应关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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