无法在AlertDialog

时间:2016-02-02 09:11:54

标签: java android android-layout android-alertdialog

我的应用中有一个包含EditText字段的AlertDialog。此对话框需要以编程方式生成,并且应与当用户通过触摸EditTextPreference编辑文本时由Android自动显示的EditTextPreference对话框外观相匹配。这一切都有效,但以编程方式插入的EditText的大小太宽,与触摸时EditTextPreference显示的大小不匹配。以下两张图片显示了问题。

使用setView()添加到AlertDialog时,

EditText看起来像这样:

EditText looks like this when added to the AlertDialog using setView()

但应该是这样的:

Should look like this

以下是XML中负责EditTextPreference的XML代码:

<EditTextPreference
            android:title="Enter Name"
            android:key="name"
            android:defaultValue=""
            android:summary=""
            android:inputType="textCapSentences|textMultiLine"
            android:singleLine="false"
            android:gravity="top|left"
            android:lines="2"
            android:minLines="1"
            android:maxLines="2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            />

负责我的对话的Java代码:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(mainActivity);
alertDialog.setTitle("Enter Date");

final EditText input = new EditText(mainActivity);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
input.setLayoutParams(lp);
input.setGravity(android.view.Gravity.TOP|android.view.Gravity.LEFT);
input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
input.setLines(1);
input.setMaxLines(1);
input.setText(lastDateValue);
alertDialog.setView(input);

5 个答案:

答案 0 :(得分:10)

您必须先添加容器,例如LinearLayout

AlertDialog.Builder alertDialog = new AlertDialog.Builder(mainActivity);
alertDialog.setTitle("Enter Date");

LinearLayout container = new LinearLayout(this);
container.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
final EditText input = new EditText(mainActivity);
input.setLayoutParams(lp);
input.setGravity(android.view.Gravity.TOP|android.view.Gravity.LEFT);
input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
input.setLines(1);
input.setMaxLines(1);
input.setText(lastDateValue);
container.addView(input, params);

alertDialog.setView(container);

我希望它有所帮助:)

答案 1 :(得分:0)

AlertDialog.Builder alertDialog = new AlertDialog.Builder(mainActivity);
alertDialog.setTitle("Enter Date");

final EditText input = new EditText(mainActivity);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
input.setLayoutParams(lp);
input.setGravity(android.view.Gravity.TOP|android.view.Gravity.LEFT);
input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
input.setLines(1);
input.setMaxLines(1);
input.setText(lastDateValue);
alertDialog.setView(input);

请更改上面的代码并提供所有边距

答案 2 :(得分:0)

您可以执行此操作而无需额外的容器布局,只需将其添加到alertDialog中即可:

alertDialog.setOnShowListener(dialog -> {
    ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) editText.getLayoutParams();
    p.setMargins(50, 20, 50, 20); // You can change the margins for your need
    editText.requestLayout();
});

答案 3 :(得分:0)

使用所需的格式化元素创建 Android XML。扩充 XML/视图并将视图添加到对话框按钮。

这样 UI 设计和代码就更清晰地分开了。带有预览功能的 Android XML 编辑器可以更轻松地获得所需的光学结果。

示例使用数据绑定,因此 XML/视图通过数据绑定 DialogEditTextBinding.inflate([...]) 寻址,结果通过数据绑定 dialogBinding.editText.getText().toString() 访问。

片段/活动中的 Java 代码

final DialogEditTextBinding dialogBinding = DialogEditTextBinding.inflate(getLayoutInflater());

AlertDialog.Builder alertDialog = new AlertDialog.Builder(mainActivity);
alertDialog.setTitle("Enter Date");

alertDialog.setView(dialogBinding);

alertDialog.setPositiveButton("OK",
    (dialog, whichButton) -> {
        String editText = dialogBinding.editText.getText().toString();
        doSomething(editText);
    });

用于对话框额外元素的 Android XML

dialog_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="22dp"
            android:layout_marginEnd="22dp"
            android:textSize="12sp"
            />

    </LinearLayout>
</layout>

PS

PS:这个例子是我的代码和你的代码组合出来的,所以它可能包含错误。

答案 4 :(得分:0)

基于@LeonardoSibela 的回答,我在 kotlin 中创建了这个简单的函数来显示一个带有编辑文本和回调的对话框。

    private fun showEditDialog(title: String, preFill: String, onSubmit: (text: String) -> Unit) {
        val editText = EditText(requireContext()).apply {
            setText(preFill)
        }
        val viewContainer = LinearLayout(requireContext()).apply {
            orientation = LinearLayout.VERTICAL
            val lp = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
            ).apply {
                setMargins(16.dp, 0, 16.dp, 0)
            }
            addView(editText, lp)
        }
        MaterialAlertDialogBuilder(requireContext())
            .setView(viewContainer)
            .setTitle(title)
            .setPositiveButton("Ok") { _, _ -> onSubmit(editText.text.toString()) }
            .setNegativeButton("Cancel") { _, _ -> }
            .show()
    }

你可以这样使用它:

showEditDialog("Change name", "") { name ->
   viewModel.updateName(name)
}