programing

Android Word-Wrap 편집 텍스트 텍스트

easyjava 2023. 9. 11. 22:25
반응형

Android Word-Wrap 편집 텍스트 텍스트

텍스트 편집 상자를 워드랩으로 이동하려고 했지만 할 수 없는 것 같습니다.

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

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

여기 내 레이아웃 파일에 있는 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씨가 이에 대해 더 좋은 조언을 해주셔서 감사합니다.그는 부울 스크롤Horizontal 속성을 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"당신을 위하여EditTextxml에 저장합니다.저는 이게 효과가 있을 거라고 확신합니다.

처음에 한 줄만 가지려고 한 다음 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="textMultiLine"

또한 높이를 설정하거나 편집 텍스트의 최대 선을 특정 값으로 설정하면 많은 문자를 입력하면 스크롤됩니다.

난 이해했다.대사가.

android:inputType="textCapSentences"

문제가 됐습니다.이 줄을 EditText 객체에 추가하면 객체가 한 줄의 EditText가 되며 단어가 줄을 긋지 않거나 'Enter'를 눌러 줄 피드나 캐리지를 EditText에 삽입할 수 있습니다.

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

편집 텍스트가 세로로 스크롤하면서 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" > 

이렇게 해보세요. 중력을 상단 왼쪽으로 바꾸는 것이 저에게 효과가 있었고, 입력 유형의 텍스트 멀티라인도 함께 사용할 수 있었습니다.

<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 텍스트 편집 다중줄이 작동하지 않습니다.그것이 제 문제였습니다.

또한 부모 레이아웃(Layout_width 속성)을 설정합니다.를 들어 layout_width=250sp

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

이거 먹어봐요.제 편집 텍스트에서 작동합니다.

 android:gravity="top|start"

- : 1 - 를 에 대한 사용자 클래스를 .EditText단어 랩

안드로이드는 이 속성이 없습니다.그러나 모든 깨지는 문자를 교체 변환 방법으로 바꿀 수 있습니다.

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;
    }
}

2단계 - 활동에서 코드 아래에 기록합니다.

Android의 경우:

myEditText.setTransformationMethod(WordBreakTransformationMethod.getInstance());

코틀린에서:

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

반응형