更改alertBox中显示的EditText(任何视图)的大小?

时间:2016-11-08 06:50:10

标签: java android android-alertdialog

我想显示一个用于用户输入的alertBox。所以我在alertBox中添加了EditText View。我的alertBox的代码是:

final EditText input = new EditText(this);
            //input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            input.setX(10);
            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
            builder.setMessage("Title")
                    .setCancelable(false)
                    .setView(input)
                    .setTitle("Create new Playlist")
                    .setPositiveButton("CREATE",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // finish the current activity
                                    // AlertBoxAdvance.this.finish();
                                    playlistName = input.getText().toString();
                                    dialog.cancel();


                                }
                            })
                    .setNegativeButton("CANCEL",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // cancel the dialog box
                                    dialog.cancel();
                                }
                            });
            android.app.AlertDialog alert = builder.create();
            alert.show();

这提供以下警告对话:

enter image description here

但是,EditText不会从标题和警报消息的开始(不正确缩进)开始。所以我想要跟随类型的EditText,它在警报对话框中有适当的缩进。

enter image description here

怎么做?如何在警报对话中改变观点的位置?或者如何改变视图的大小?

2 个答案:

答案 0 :(得分:1)

试试这个:

XML布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editTextField"
        android:layout_marginLeft="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />

</LinearLayout>

Java代码:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mActivity);
    LayoutInflater inflater = mActivity.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.dialog, null);
    dialogBuilder.setView(dialogView);

final EditText mEditText = (EditText) dialogView.findViewById(R.id.editTextField);

dialogBuilder.setTitle("Title");
dialogBuilder.setMessage("Type your message here");

dialogBuilder.setPositiveButton("Yes", null);
dialogBuilder.setNegativeButton("No", null);

final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {
        Button positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        positiveButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String enteredTextString= mEditText.getText().toString();
                //To whatever with the text entered
            }
        });
        Button negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
        negativeButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
    }
});
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {

    }
});
alertDialog.show();

答案 1 :(得分:1)

您可以尝试使用自定义视图... 在布局文件中定义EditText,比如lay.xml

然后

builder.setMessage("Title")
                .setCancelable(false)
                .setContentView(R.layout.lay);