简体   繁体   中英

Android AlertDialog multiline EditText

Is there a way to create a multiline EditText in a AlertDialog in Android . I set the setLines and it shows a bigger EditText for several lines. but when I'm typing it doesn't go to next line and still types in the same line . Here is my code.

Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Comment");

        final EditText input = new EditText(this);

        final String item_value = ItemList.get(position).get("comment");

        input.setText(item_value);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        input.setLines(5);
        input.setMaxLines(5);
        input.setGravity(Gravity.LEFT | Gravity.TOP);
        builder.setView(input);

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {


            }
        });

        builder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

and my dialog looks like this. 在此处输入图像描述

so how can I fix this. Thanks and regards.

try this code for your EditText:

input.setSingleLine(false);  //add this
input.setLines(4);
input.setMaxLines(5);
input.setGravity(Gravity.LEFT | Gravity.TOP);
input.setHorizontalScrollBarEnabled(false); //this

Use input type input.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE) and input.setSingleLine(false) . So your code will be -

Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Comment");

        final EditText input = new EditText(this);

        final String item_value = ItemList.get(position).get("comment");

        input.setText(item_value);
        input.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        input.setSingleLine(false)
        input.setLines(5);
        input.setMaxLines(5);
        input.setGravity(Gravity.LEFT | Gravity.TOP);
        builder.setView(input);

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {


            }
        });

        builder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

This is working for me. you can use this property for you runtime EditText.

<EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top|left"
        android:inputType="textMultiLine"
        android:lines="8"
        android:maxLines="10"
        android:minLines="6"
        android:scrollbars="vertical" />
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Comment");

final EditText input = new EditText(this);

final String item_value = ItemList.get(position).get("comment");

input.setText(item_value);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setSingleLine(false);
input.setMaxLines(5);
input.setGravity(Gravity.LEFT | Gravity.TOP);
builder.setView(input);

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialog, int whichButton) {

    }
});

builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
    }
});
AlertDialog alert = builder.create();
alert.show();

Kotlin version wrapped in DialogFragment:

import android.app.Dialog
import android.os.Bundle
import android.text.InputType
import android.view.Gravity
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.AppCompatEditText
import androidx.fragment.app.DialogFragment

class MultilineEditDialog : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val input = AppCompatEditText(requireContext()).apply {
            inputType = InputType.TYPE_TEXT_FLAG_MULTI_LINE
            isSingleLine = false
            setLines(3)
            maxLines = 3
            gravity = Gravity.START and Gravity.TOP
            hint = "Hint"
        }
        return AlertDialog.Builder(requireContext()).apply {
            setTitle("Title")
            setView(input)
            setPositiveButton(android.R.string.ok) { _, _ ->
                // TODO take an action
            }
            setNegativeButton(android.R.string.cancel, null)
        }.create()
    }
}
input.setSingleLine(false); 

will make your edit text to increase as per data

Set attribute android:inputType="textMultiLine" in your XML.

If it doesn't work, You have to implement TextWatcher and manually break lines by yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM