IT박스

Android Word-Wrap EditText 텍스트

itboxs 2020. 11. 3. 07:50
반응형

Android Word-Wrap EditText 텍스트


내 EditText 상자를 단어 줄 바꿈으로 만들려고했지만 할 수없는 것 같습니다.

나는 안드로이드 애플리케이션을 개발하면서 훨씬 더 복잡한 문제를 다루었는데, 이것은 간단한 과정이어야 할 것 같다.

그러나 문제는 여전히 남아 있으며 텍스트를 입력 할 때 가로로 스크롤하면서 한 줄로만 텍스트를 입력 할 수있는 큰 텍스트 상자가 있습니다.

다음은 내 레이아웃 파일의 EditText 개체에 대한 XML 코드입니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/myWidget48"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
 <ScrollView
    android:id="@+id/myScrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    >
    <LinearLayout
        android:id="@+id/widget37"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
    >

<EditText
        android:id="@+id/txtNotes"
        android:layout_width="300px"
        android:layout_height="120px"
        android:scrollbars="vertical"
        android:textSize="18sp"
        android:gravity="left"
        android:layout_marginTop="10dip"
        android:inputType="textCapSentences"
        >
    </EditText>
</LinearLayout>
</ScrollView>
</LinearLayout>

문제의 원인을 찾는 것 외에도 해결책을 찾았습니다. android:inputType를 사용하는 경우 여러 줄 지원을 활성화하려면 textMultiLine을 사용해야합니다. 또한 inputType을 사용하면 코드를 대체합니다 android:singleLine="false". inputType을 사용하는 경우 반복하려면 textMultiLine을 사용해야합니다. 그렇지 않으면 EditText 객체가 단어 줄 바꿈없이 한 줄로만 구성됩니다.

편집 : 이에 대해 더 좋은 조언을 해주신 Jacob Malliet 에게 감사드립니다 . 그는 부울 scrollHorizontally 속성을 false로 설정하도록 제안했습니다 'android:scrollHorizontally="false"'.

XML 코드 예 :

<EditText
    android:id ="@+id/edtInput"
    android:layout_width ="0dip" 
    android:layout_height ="wrap_content" 
    android:layout_weight ="1" 
    android:inputType="textCapSentences|textMultiLine"
    android:maxLines ="4" 
    android:maxLength ="2000" 
    android:hint ="@string/compose_hint"
    android:scrollHorizontally="false" />

좋아, 난 당신이 설정해야합니다, 그것을 알아 냈 android:scrollHorizontally="false"당신을 위해 EditText당신의 XML로. 이것이 효과가있을 것이라고 확신합니다.


처음에 한 줄만 사용하고 5 줄로 확장하고 최대 10 줄을 원한다고 가정합니다.

<EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/etMessageBox"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:autoLink="all"
                    android:hint="@string/message_edit_text_hint"
                    android:lines="5"
                    android:minLines="1"
                    android:gravity="top|left"
                    android:maxLines="10"
                    android:scrollbars="none"
                    android:inputType="textMultiLine|textCapSentences"/>

증가함으로써 android:lines당신은 얼마나 많은 줄을 확장 할 수 있습니다.


나는 그것을 알아. 라인,

android:inputType="textCapSentences"

문제였습니다. 이 줄을 EditText 개체에 추가하면 개체가 한 줄 EditText가되고 단어 줄 바꿈이 허용되지 않으며 사용자가 'Enter'키를 눌러 EditText에 줄 바꿈 또는 캐리지 리턴을 삽입 할 수 있습니다.


편집 텍스트에 다음 속성을 추가해야합니다.

android:inputType="textMultiLine"

또한 높이를 설정하거나 edittext의 maxlines를 특정 값으로 설정하면 많은 문자를 입력하면 스크롤됩니다.


전체 활동을 스크롤보기로 묶고 다음 편집 텍스트를 선형 레이아웃으로 배치하는 것은 나에게 매력처럼 작동했습니다.

편집 텍스트는 다음과 같이 Enter 키를 누르면 수직으로 스크롤됩니다.

<EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="Enter somehting"
            android:inputType="textMultiLine"
            android:maxLength="2000"
            android:maxLines="6"
            android:scrollHorizontally="false"
            android:scrollbars="vertical" > 

입력 유형 textmultiline과 함께 중력을 top | left로 변경하는 것이 효과적이었습니다.

<EditText
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/color_red"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="top|left"
    android:inputType="textMultiLine|textCapSentences"
    android:lines="10"
    android:maxWidth="200dp"
    android:maxLength="200"
    android:maxLines="10"
    android:singleLine="false"
    android:textColor="@android:color/white"
    android:textSize="26sp" />

실용적으로 수행하는 사람들을 위해 입력 유형을 올바르게 설정하고 있는지 다시 확인 해야 하기 때문에 Android EditText Multiline이 작동하지 않는다는 질문을 볼 수 있습니다 . 그게 내 문제 였어


또한 부모 레이아웃 너비 , (Layout_width 속성)을 설정 합니다. 예 : layout_width = 250sp

그렇지 않으면 EditText 또는 TextView가 모바일 테두리의 끝에 도달 할 때까지 다음 줄로 줄 바꿈하지 않습니다. 따라서 Layout_width를 정의하십시오.


참고 : 1 단계-editText wordWrap에 대한 사용자 정의 클래스를 만듭니다.

Android에는이 속성이 없습니다. 그러나 모든 구분 문자를 ReplacementTransformationMethod로 바꿀 수 있습니다.

public class WordBreakTransformationMethod extends ReplacementTransformationMethod
{
    private static WordBreakTransformationMethod instance;
    private WordBreakTransformationMethod() {}

    public static WordBreakTransformationMethod getInstance()
    {
        if (instance == null)
        {
            instance = new WordBreakTransformationMethod();
        }
        return instance;
    }

    private static char[] dash = new char[] {'-', '\u2011'};
    private static char[] space = new char[] {' ', '\u00A0'};

    private static char[] original = new char[] {dash[0], space[0]};
    private static char[] replacement = new char[] {dash[1], space[1]};

    @Override
    protected char[] getOriginal()
    {
        return original;
    }

    @Override
    protected char[] getReplacement()
    {
        return replacement;
    }
}

step 2 - In Activity , write below code,

Android에서 :

myEditText.setTransformationMethod(WordBreakTransformationMethod.getInstance());

Kotlin에서 :

myEditText.setTransformationMethod= WordBreakTransformationMethod.getInstance

XML에서 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textCapSentences|textMultiLine"
        android:scrollHorizontally="false" />
</LinearLayout>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/table"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    >  
     <TableRow
        android:id="@+id/newRow">
        <LinearLayout
             android:layout_width="fill_parent"
             android:orientation="vertical"
             android:layout_height="wrap_content"
             android:gravity="left"
             android:paddingBottom="10dip">
            <TextView  
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Some Text"
            />
        </LinearLayout>
        </TableRow>

        <View
        android:layout_height="2dip"
        android:background="#FF909090" />

    <TableRow>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="10dip">

    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"         
        android:text="Some Text"
        android:paddingBottom="5dip"
        />     
    <EditText 
        android:id="@+id/editbox"  
        android:layout_width="wrap_content" 
        android:layout_height="150px"
        android:gravity="top"   
        android:inputType="textFilter"
        android:scrollHorizontally="false"      
        />  

</RelativeLayout>
</TableRow>
<TableRow>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button 
        android:id="@+id/btnone"  
        android:layout_width="3dip" 
        android:layout_height="wrap_content"
        android:layout_margin="2dip"
        android:layout_weight="1"
        android:text="Btn"
        />          
    <Button 
        android:id="@+id/btntwo"  
        android:layout_width="3dip" 
        android:layout_height="wrap_content"
        android:layout_margin="2dip"
        android:layout_weight="1"
        android:text="Btn"
        />    
 </LinearLayout>
 </TableRow>
 </TableLayout>

참고 URL : https://stackoverflow.com/questions/3276380/android-word-wrap-edittext-text

반응형