Android의 EditText보기에서 여러 줄을 허용합니까?


답변:


1175

기본적 EditText으로 Android의 모든 위젯은 여러 줄로 표시됩니다.

샘플 코드는 다음과 같습니다.

<EditText
    android:inputType="textMultiLine" <!-- Multiline input -->
    android:lines="8" <!-- Total Lines prior display -->
    android:minLines="6" <!-- Minimum lines -->
    android:gravity="top|left" <!-- Cursor Position -->
    android:maxLines="10" <!-- Maximum Lines -->
    android:layout_height="wrap_content" <!-- Height determined by content -->
    android:layout_width="match_parent" <!-- Fill entire width -->
    android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>

6
inputType을 textMultiLine으로 설정하고 imeOptions를 actionSend로 설정하는 방법이 있습니까? inputType을 textMultiLine으로, imeOptions를 actionSend로 설정하면 send 대신 Enter 키가 S4 키보드에 표시됩니다.
— user484691

1
"textMultiLine 및 imeOptions"에 대해서는 stackoverflow.com/questions/5014219/…
— TOMKA 8:18의

2
한가지주의 할 점 inputType은 적어도 linesand minLines속성을 무효화하는 것입니다. 나는 가지고 있었고 inputType="phone"라인 속성은 영향을 미치지 않았다
— ono

2
이것을 사용하지 마십시오. 는 원인 IndexOutOfBoundsException이 때문에 뭔가를 입력하는 즉시android:inputType="textMultiLine"
— Chisko

AppCompactTextView를 사용하는 경우 android : inputType = "textMultiLine"행이 필요합니다.
— sourav

260

다음을 사용하는 것이 좋습니다.

<EditText 
...
android:inputType="textMultiLine"
/>

android:singleLine더 이상 사용되지 않기 때문 입니다.


Sharduls의 대답은 분명히 현대 Apis에서 작동해야합니다. 더 이상 사용되지 않는 singleLine부분을 삭제했기 때문에 (오래 전에). 질문 자체에 관해서는 이것이 답변의 가장 중요한 부분입니다.
— 크노소스

이것을 사용하지 마십시오. 원인 IndexOutOfBoundsException당신이 뭔가를 입력하는 즉시. 여러 줄이 기본값입니다.
— Chisko

@Chisko XML 특성으로 인해 IndexOutOfBoundsException이 발생하지 않습니다. 기본 inputType이 언제 변경되었는지 기억조차하지 않지만 (짝수 했습니까?) 이전 버전과의 호환성을 위해 이것을 유지하는 것이 가장 좋습니다.
— Knossos

1
@Knossos 잘, 그것은 당신을 위해 이상한 그것은 나를 위해입니다. 나는 문자 그대로 코드를 복사하여 붙여 넣기 전까지 여러 매개 변수의 가능한 모든 조합으로 충돌했습니다. 그림을 이동.
— Chisko

42

이것은 나를 위해 실제로 작동합니다. 실제로 두 가지 속성 인 inputType 및 lines 이 중요 합니다 . 또한 스크롤 막대가 필요할 수 있습니다. 아래 코드는 스크롤 막대를 만드는 방법을 보여줍니다.

 <EditText
        android:id="@+id/addr_edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top|left"
        android:inputType="textEmailAddress|textMultiLine"
        android:lines="20"
        android:minLines="5"
        android:scrollHorizontally="false"
        android:scrollbars="vertical" />

20

이것이 아래 코드 스 니펫을 적용한 방법이며 정상적으로 작동합니다. 희망, 이것은 누군가를 도울 것입니다.

<EditText 
    android:id="@+id/EditText02"
    android:gravity="top|left" 
    android:inputType="textMultiLine"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:lines="5" 
    android:scrollHorizontally="false" 
/>

건배! ...감사.


11

이것을 시도하고 편집 텍스트보기 에이 줄을 추가하십시오. 내 것을 추가하겠습니다. 당신이 그것을 이해해야합니다

android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"

<EditText
    android:inputType="textMultiLine"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText_newprom_description"
    android:padding="10dp"
    android:lines="5"
    android:overScrollMode="always"
    android:scrollbarStyle="insideInset"
    android:minLines="5"
    android:gravity="top|left"
    android:scrollbars="vertical"
    android:layout_marginBottom="20dp"/>

그리고 당신의 자바 클래스에서 다음과 같이이 편집 텍스트에 클릭 리스터를 만드십시오.

EditText description;
description = (EditText)findViewById(R.id.editText_newprom_description);

description.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                view.getParent().requestDisallowInterceptTouchEvent(true);
                switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){
                    case MotionEvent.ACTION_UP:
                        view.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }

                return false;
            }

        });

이것은 나를 위해 잘 작동


감사합니다! 이것이 스크롤 가능한 뷰 내에서 편집 텍스트를 스크롤 가능하게 만드는 방법입니다.
— Esdras Lopez


5

이것들은 모두 훌륭하지만 상위 스크롤보기 안에 편집 텍스트가있는 경우에는 작동하지 않습니다 :) 아마도 가장 일반적인 예는 항목이 너무 많아 보이는 영역을 벗어나는 "설정"보기입니다. 이 경우 설정을 스크롤 가능하게 만들기 위해 모두 스크롤보기에 넣습니다. 설정에 여러 줄 스크롤 가능 편집 텍스트가 필요한 경우 해당 스크롤이 작동하지 않습니다.


5
 <EditText
           android:id="@id/editText" //id of editText
           android:gravity="start"   // Where to start Typing
           android:inputType="textMultiLine" // multiline
           android:imeOptions="actionDone" // Keyboard done button
           android:minLines="5" // Min Line of editText
           android:hint="@string/Enter Data" // Hint in editText
           android:layout_width="match_parent" //width editText
           android:layout_height="wrap_content" //height editText
           /> 

4

테마에 이전에 지정된 행 수를 사용하지 않으려면 xml 속성을 사용하십시오. android:lines="@null"


4
android:inputType="textMultiLine"

이 코드 라인은 저에게 효과적입니다. 이 코드를 편집 한 텍스트를 XML 파일에 추가하십시오.


3

android:inputType="textMultiLine" 관련 필드의 XML 파일에 이것을 추가하십시오 .


1

나는 웹 사이트를 좋아하지 않지만 http://www.pcsalt.com/android/edittext-with-single-line-line-wrapping-and-done-action-in-android/ 에서 이것을 배웠습니다 . 여러 줄로 입력하지만 Enter 버튼을 게시 버튼으로 유지하려면 목록보기의 "가로 스크롤"을 false로 설정하십시오.

android:scrollHorizontally="false"

XML에서 작동하지 않으면 프로그래밍 방식으로 이상하게 작동합니다.

listView.setHorizontallyScrolling(false);


1
<EditText
                android:hint="Address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:inputType="textMultiLine|textNoSuggestions"
                android:id="@+id/edittxtAddress"
                android:layout_marginLeft="@dimen/textsize2"
                android:textColor="@android:color/white"
                android:scrollbars="vertical"
                android:minLines="2"
            android:textStyle="normal" />

활동에서 "\ n"을 제거하려면 다음 행을 누르십시오.

String editStr= edittxtAddress.getText().toString().trim();

 MyString= editStr.replaceAll("\n"," ");

1

일부가 AppCompatEditText 를 사용하는 경우 inputType = "textMultiLine" 을 설정해야합니다. 여기 복사 할 수있는 코드가 있습니다.


   <androidx.appcompat.widget.AppCompatEditText
        android:padding="@dimen/_8sdp"
        android:id="@+id/etNote"
        android:minLines="6"
        android:singleLine="false"
        android:lines="8"
        android:scrollbars="vertical"
        android:background="@drawable/rounded_border"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_25sdp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="@dimen/_25sdp"
        android:autofillHints=""

        android:gravity="start|top"
        android:inputType="textMultiLine"
         />

배경

   <?xml version="1.0" encoding="utf-8"?>
   <shape android:shape="rectangle" 
    xmlns:android="http://schemas.android.com/apk/res/android">
   <corners android:radius="@dimen/_5sdp"/>
   <stroke android:width="1dp" android:color="#C8C8C8"/>
   </shape>

0

해야 할 또 다른 깔끔한 일은와 android:minHeight함께 사용 하는 것 android:layout_height="wrap_content"입니다. 이 방법을 사용하면 비어있을 때 편집 크기를 설정할 수 있고 사용자가 물건을 입력 할 때 여러 줄을 포함하여 사용자 입력량에 따라 늘어납니다.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.