완료 작업 버튼이있는 여러 줄 EditText 동시에 가질

세트 가있는 EditText위젯 을 동시에 가질 수 있습니까?android:inputType="textMultiLine"android:imeOptions="actionDone"

키보드의 작업 버튼이 Enter (캐리지 리턴)가 아닌 완료로 된 여러 줄 편집 상자를 원하지만 작동하지 않는 것 같습니다 ..

미리 감사드립니다



답변

사용하다

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);

그리고 XML :

android:inputType="textMultiLine"


답변

Android 문서에서 : ‘ “textMultiLine”사용자가 줄 바꿈 (캐리지 리턴) 을 포함하는 긴 텍스트 문자열을 입력 할 수있는 일반 텍스트 키보드 . ‘따라서 키보드에서’완료 ‘버튼을 사용하려는 경우 textMultiLine 속성이 적합하지 않습니다.

done 버튼으로 여러 줄 (이 경우 3 줄) 입력 필드를 얻는 간단한 방법은 다음과 함께 EditText를 사용하는 것입니다.

android:lines="3"
android:scrollHorizontally="false" 

그러나 어떤 이유로 이것은 레이아웃 파일 (onCreate에서) 대신 코드에서 이러한 설정을 수행하는 경우에만 작동합니다.

TextView tv = (TextView)findViewById(R.id.editText);
if (tv != null) {
    tv.setHorizontallyScrolling(false);
    tv.setLines(3);
}

알아내는 데 시간이 꽤 걸렸기 때문에 누군가에게 도움이되기를 바랍니다. 매니페스트에서 작동하도록하는 방법을 찾으면 알려주십시오.


답변

작업 예! 이 기능을 지원하는 아래 사용자 정의 EditText 클래스를 만들고 xml 파일의 클래스를 사용합니다. 작동 코드 :

package com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.EditText;

public class ActionEditText extends EditText
{
   public ActionEditText(Context context)
   {
       super(context);
   }

   public ActionEditText(Context context, AttributeSet attrs)
   {
       super(context, attrs);
   }

   public ActionEditText(Context context, AttributeSet attrs, int defStyle)
   {
       super(context, attrs, defStyle);
   }

   @Override
   public InputConnection onCreateInputConnection(EditorInfo outAttrs)
   {
       InputConnection conn = super.onCreateInputConnection(outAttrs);
       outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
       return conn;
   }
}

<com.example.ActionEditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:imeOptions="actionDone"
       android:inputType="textAutoCorrect|textCapSentences|textMultiLine" />


답변

Kotlin에서이 작업을 수행하려면 (선택적 textCapSentences으로이 확장 기능을 사용할 수있는 다른 구성을 적용 할 수도 있습니다.)

// To use this, do NOT set inputType on the EditText in the layout
fun EditText.setMultiLineCapSentencesAndDoneAction() {
    imeOptions = EditorInfo.IME_ACTION_DONE
    setRawInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES or InputType.TYPE_TEXT_FLAG_MULTI_LINE)
}

용법:

myEditText.setMultiLineCapSentencesAndDoneAction()


답변

나는 이것이 당신의 일을하는 방법이라고 생각합니다. 데 android:inputType="textMultiLine", android:imeOptions="actionDone"차종은 키 기능이 모호한 입력합니다. 을 (를) 사용 android:lines="10"하고 제거 할 수 있다는 점을 명심 하십시오 android:inputType="textMultiLine". 그러나 달성하려는 내용에 따라를 필요로하며 android:inputType="textMultiLine"대체 할 수 없습니다.

EditText ed=new EditText(this);
ed.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_ENTER){
                //do your stuff here
            }
            return false;
        }
});


답변

이것은 나를 위해 완벽하게 작동하는 것 같습니다.

int lineNum = 2;
mEditText.setHorizontallyScrolling(false);
mEditText.setLines(3);


답변

재사용 가능한 Kotlin 솔루션

코드에서 이러한 값 설정하는 것이 저에게 효과적이었습니다.

edittext.inputType = EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE
edittext.setHorizontallyScrolling(false)
edittext.maxLines = Integer.MAX_VALUE // Or your preferred fixed value

나는 이것을 자주 요구하므로 코드를 깨끗하게 유지하기 위해 이것을 만들었습니다.

fun EditText.multilineIme(action: Int) {
    imeOptions = action
    inputType = EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE
    setHorizontallyScrolling(false)
    maxLines = Integer.MAX_VALUE
}

// Then just call
edittext.multilineIme(EditorInfo.IME_ACTION_DONE)

‘완료’에 선택적 사용자 지정 작업을 추가하려면 다음을 시도하십시오.

fun EditText.multilineDone(callback: (() -> Unit)? = null) {
    val action = EditorInfo.IME_ACTION_DONE
    multilineIme(action)
    setOnEditorActionListener { _, actionId, _ ->
            if (action == actionId) {
                callback?.invoke()
                true
            }
            false
        }
    }
}

// Then you can call
edittext.multilineDone { closeKeyboard() }

// or just
edittext.multilineDone()

콜백에서 키보드를 쉽게 제어해야합니까? 이 게시물 읽기

그런 다음 hideKeyboard()전화 추가EditText.multilineDone