programing

편집 텍스트를 한 줄로 제한

i4 2023. 9. 13. 22:19
반응형

편집 텍스트를 한 줄로 제한

중복 가능: 안드로이드-단일행-참-편집용 텍스트

<EditText 
    android:id="@+id/searchbox"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:lines="1"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:layout_weight="1"
    android:layout_marginTop="2dp"
    android:drawablePadding="10dp"
    android:background="@drawable/edittext"
    android:drawableLeft="@drawable/folder_full"
    android:drawableRight="@drawable/search"
    android:paddingLeft="15dp"
    android:hint="search...">
</EditText>

를 만들고 싶습니다.EditText.사용자가 "입력"을 눌러도 커서가 두 번째 줄로 내려가지 않아야 합니다.누가 그렇게 하는 것 좀 도와줄 수 있습니까?

사용하다android:maxLines="1"그리고.android:inputType="text"

Android:maxLines 특성을 잊어버렸습니다.Android:inputType을 참조하십시오. 예를 들어 아래에서 이 결과를 제공합니다.

<EditText 
    android:id="@+id/searchbox"  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:inputType="text"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:layout_weight="1"
    android:layout_marginTop="2dp"
    android:drawablePadding="10dp"
    android:background="@drawable/edittext"
    android:drawableLeft="@drawable/folder_full"
    android:drawableRight="@drawable/search"
    android:paddingLeft="15dp"
    android:hint="search...">
</EditText>

을 해서.android:singleLine="true"사용되지 않습니다.

입력 유형을 추가하고 maxline을 1로 설정하면 모든 것이 잘 작동합니다.

android:inputType="text"
android:maxLines="1"

android:singleLine를 사용하지 않습니다.설명서에서:

이 상수는 API 레벨 3에서 더 이상 사용되지 않습니다.

이 속성은 더 이상 사용되지 않습니다. 사용maxLines텍스트의 신적을고다의e고d 를 사용합니다.textMultiLine편집 가능한 텍스트 보기 대신 inputType 특성에 플래그를 지정합니다(singleLine과 inputType이 모두 제공되는 경우 inputType 플래그가 singleLine의 값을 재정의함).

그래서 당신은 그냥 설정할 수 있어요.android:inputType="textEmailSubject"또는 필드 내용과 일치하는 다른 값을 입력합니다.

다음 행을 xml의 EditText에 추가해야 합니다.

android:maxLines="1"
android:maxLines="1"
android:inputType="text"

레이아웃에서 EditText 태그에 한 줄을 두려면 위 코드를 추가합니다.

android:singleLine="true" is deprecated

이 상수는 API 레벨 3에서 더 이상 사용되지 않습니다.

이 특성은 더 이상 사용되지 않습니다.maxLines를 사용하여 정적 텍스트의 레이아웃을 변경하고 편집 가능한 텍스트 보기 대신 inputType 특성의 textMultiLine 플래그를 사용합니다(singleLine과 inputType이 모두 제공되는 경우 inputType 플래그가 singleLine의 값을 재정의함).

지금이다android:singleLine속성이 더 이상 사용되지 않습니다.이 을 당신의에에 .EditText당분간은EditText외줄로.

android:inputType="text"
android:imeOptions="actionNext"
android:maxLines="1"

이것은 EditText에 대해 작동합니다.

android:inputType="text"

그런 다음 입력 텍스트의 최대 길이를 설정합니다.

android:maxLines="1"

그것이 지금 필요한 두 가지입니다.

저는 이것을 과거에 한 적이 있습니다.android:singleLine="true" "키에 합니다. "Enter" 키에 대한 수신자를 추가합니다.

((EditText)this.findViewById(R.id.mytext)).setOnKeyListener(new OnKeyListener() {

    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
            //if the enter key was pressed, then hide the keyboard and do whatever needs doing.
            InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);

            //do what you need on your enter key press here

            return true;
        }

        return false;
    }
});

android:singleLine="true"

모든 사람들이 XML 방식을 보여주고 있습니다. 한 사람이 편집을 호출한 것을 제외하고요.텍스트의 setMaxLines 메서드입니다.하지만 막상 해보니 효과가 없었습니다.한 가지 효과가 있었던 것은 입력 유형을 설정하는 것이었습니다.

EditText editText = new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_TEXT);

이렇게 하면 A-Z, a-z, 0-9 및 특수 문자를 사용할 수 있지만 입력을 누를 수는 없습니다.Enter 키를 누르면 응용 프로그램에서 해당되는 경우 다음 GUI 구성 요소로 이동합니다.

You may also want to set the maximum number of characters that can be put into that EditText, or else it'll push whatever's to the right of it off the screen, or just start trailing off the screen itself. You can do this like this:

InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(8);
editText.setFilters(filters);

This sets the max characters to 8 in that EditText. Hope all this helps you.

You must add this line in your EditText

android:maxLines="1"

그리고 또 하나, 세트를 잊지 마.android:inputType(원하는 문자, 전화.. 하지만 반드시 설정해야 합니다.)

Programatically:

textView.setMaxLines(1);

In order to restrict you just need to set the single line option on "true".

android:singleLine="true"

use

android:ellipsize="end"
android:maxLines="1"

The @Aleks G OnKeyListener() works really well, but I ran it from MainActivity and so had to modify it slightly:

EditText searchBox = (EditText) findViewById(R.id.searchbox);
searchBox.setOnKeyListener(new OnKeyListener() {

    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
            //if the enter key was pressed, then hide the keyboard and do whatever needs doing.
            InputMethodManager imm = (InputMethodManager) MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(searchBox.getApplicationWindowToken(), 0);

            //do what you need on your enter key press here

            return true;
        }

        return false;
    }
});

I hope this helps anyone trying to do the same.

Also it's important to pay attention if the property android:inputType is textMultiLine. If so, the properties android:singleLine, android:imeOptions, android:ellipsize and android maxLines will be ignored and your EditText will accepted MultiLines anyway.

Add this line to your edittext

android:inputType="text"

android:imeOptions="actionDone"

worked for me.

please try this code

android:inputType="textPersonName"

Use Below Code instead of your code

<EditText 
    android:id="@+id/searchbox"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:inputType="text"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:layout_weight="1"
    android:layout_marginTop="2dp"
    android:drawablePadding="10dp"
    android:background="@drawable/edittext"
    android:drawableLeft="@drawable/folder_full"
    android:drawableRight="@drawable/search"
    android:paddingLeft="15dp"
    android:hint="search..."/>

~하듯이android:singleLine="true"이제 감가 상각되므로 사용하십시오.

android:maxLines="1"

Use maxLines instead to change the layout of a static text, and use the textMultiLine flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).

in XML File add this properties in Edit Text

 android:maxLines="1"

At XML file. Just add

android:maxLines="1"

for me i do it like this , go to your textView in xml file and add this two lines

` android:maxLines="1"

android:inputType="text"`

위의 모든 것에 대해 한 가지 더 언급합니다.수치 입력 타입이 필요하시면 그냥 사용하세요.inputType="number". 하지만 만약 당신이 사용한다면.android:digits="0123456789"다음과 같은 속성으로는 필요한 효과를 얻을 수 없습니다.lines,maxLines,inputType

android:singleLine="true" android:maxLines="1"

ReferenceURL : https://stackoverflow.com/questions/10978038/restrict-edittext-to-single-line

반응형