【问题标题】:Are there built in dialogs for input for text, lists, etc... like builtin preference screens?是否有用于输入文本、列表等的内置对话框,例如内置首选项屏幕?
【发布时间】:2015-09-02 08:47:56
【问题描述】:

在首选项片段中,当您单击 EditTextPreference 或 ListPreference 或其他项目时...它会自动打开一个对话框,允许根据您的设置进行输入或选择。

如果您不使用 PrefFrag.... 而是编写自己的输入屏幕,是否可以调用任何内置对话框来执行此操作,还是我们必须从头开始创建自己的对话框?

【问题讨论】:

    标签: android dialog android-preferences


    【解决方案1】:

    您必须自己构建它们,尽管大部分代码都可以从本文中轻松复制,并稍作更改:http://developer.android.com/guide/topics/ui/dialogs.html

    其中包括从列表中选择一个项目。对于文本字段,您需要做一些稍微不同的事情。这是一个例子。在 Activity 中使用它:

        //First, set these variable to what you want
        String title = "ENTER A TITLE HERE";
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
    
        // Set up the input
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);
    
        // Set up the buttons
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String text = input.getText().toString();
                //text is the String the user inputted, use it for what you need here
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //This means the user hit Cancel instead of OK
            }
        });
    
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    

    【讨论】:

      【解决方案2】:

      是的,您必须从头开始创建它。嗯.. 不完全是从零开始,因为你有AlertDialog.BuilderDialogFragmentProgressDialogDatePickerDialogTimePickerDialog。创建您的对话框。请查看对话框documentations

      但与首选项不同,xml 元素不会神奇地为您提供这些对话框。

      【讨论】:

        【解决方案3】:

        你可以试试this。它包含许多标准对话框,如输入文本、列表项等。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-27
          • 2012-10-09
          • 1970-01-01
          • 1970-01-01
          • 2016-04-04
          • 2022-01-21
          • 2011-04-20
          • 2017-04-27
          相关资源
          最近更新 更多