program story

Android EditView '완료'버튼을 만들고 클릭 할 때 키보드를 숨기려면 어떻게해야합니까?

inputbox 2020. 8. 10. 08:03
반응형

Android EditView '완료'버튼을 만들고 클릭 할 때 키보드를 숨기려면 어떻게해야합니까?


사용자가를 클릭하면 EditViewAndroid가 키보드를 열어 사용자가 EditView.

문제는 사용자가 쓰기를 마치면 키보드를 숨길 방법이 없다는 것입니다. 사용자는 키보드를 숨기려면 뒤로 버튼을 눌러야합니다.

Done키보드를 숨기는 버튼을 키보드에 표시하는 방법이 있습니까?


TextView.setImeOptions를 사용 하고 actionDone을 ​​전달합니다. 처럼textView.setImeOptions(EditorInfo.IME_ACTION_DONE);


먼저 아래와 같이 대상 EditText와 android:imeOptions동일한 속성 을 설정해야 actionDone합니다. 그러면 EditText 소프트 키보드의 'RETURN'버튼이 'DONE'버튼으로 변경됩니다.

<EditText 
    android:id="@+id/edittext_done"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Enter some text"
    android:imeOptions="actionDone"
    />

다음을 모두 포함하십시오 .imeOptions singleLine

<EditText 
   android:id="@+id/edittext_done"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:imeOptions="actionDone"
   android:singleLine="true"
   />

android:imeActionLabel="Done" 
android:singleLine="true"

XML 파일에서 잘 작동합니다. 그러나 이것은 또한 editText당신이 원하지 않을 수도있는 한 줄에 계속 입력하게 할 것입니다. 따라서 코드에 다음을 추가하면 모든 것을 한 줄에 입력하지 않아도됩니다.

mainText.setHorizontallyScrolling(false);
mainText.setMaxLines("Maximum integer value that you want to provide");

이것을 사용하십시오 :

android:singleLine="true"

완료 버튼

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

android:inputType="text" XML에서

키보드에서 클릭 완료 처리

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
            if(actionId == EditorInfo.IME_ACTION_DONE){
                // Your action on done
                return true;
            }
            return false;
        }
    });

`


이 두 줄을 사용하여 EditText

android:imeActionLabel="Done"
android:singleLine="true"

또는이 줄에 의해 프로그래밍 방식으로 달성 할 수 있습니다.

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

위젯에 대한 속성이 변경되지 않으면 android:imeOptions="actionDone"레이아웃 xml파일 에서 와 같이 사용하는 것이 더 나을 수 있습니다 .


사용하다:

android:imeActionLabel="Done"
android:singleLine="true" 

ActionDone은 키보드가 숨겨 졌을 때 키보드에서 다음 버튼을 클릭 할 때 사용합니다. 텍스트 편집 또는 AppcompatEdit에서 사용합니다.

XML

1.1 AppCompatEdittext를 사용하는 경우

    <android.support.v7.widget.AppCompatEditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"/>

1.2 Edittext를 사용하는 경우

    <EditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"/>

자바

EditText edittext= (EditText) findViewById(R.id.edittext);
edittext.setImeOptions(EditorInfo.IME_ACTION_DONE);

많은 사람들이 문제를 알지 못하는 사이에 어려움을 겪을 수 있다는 점을 지적해야합니다.

If you want the kb to hide when clicking Done, and you set android:imeOptions="actionDone" & android:maxLines="1" without setting your EditText inputType it will NOT work as the default inputType for the EditText is not "text" as a lot of people think.

so, setting only inputType will give you the results you desire whatever what you are setting it to like "text", "number", ...etc.


For the code:

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

Actually you can set custom text to that little blue button. In the xml file just use

android:imeActionLabel="whatever"

on your EditText.

Or in the java file use

etEditText.setImeActionLabel("whatever", EditorInfo.IME_ACTION_DONE);

I arbitrarily choose IME_ACTION_DONE as an example of what should go in the second parameter for this function. A full list of these actions can be found here.

It should be noted that this will not cause text to appear on all keyboards on all devices. Some keyboards do not support text on that button (e.g. swiftkey). And some devices don't support it either. A good rule is, if you see text already on the button, this will change it to whatever you'd want.

참고URL : https://stackoverflow.com/questions/1919742/how-do-i-make-an-android-editview-done-button-and-hide-the-keyboard-when-click

반응형