문제

내 사용자가 누를 때 입력하다 가상 Android에서 "사용자가 입력을 확인하십시오!" 키보드 내 키보드가 보이십시오! (왜?)

여기 내 Java 코드 ...

private void initTextField() {
    entryUser = (EditText) findViewById(R.id.studentEntrySalary);
    entryUser.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:
                        userValidateEntry();
                        return true;
                }
            }

          return true;
        }
    });
}

private void userValidateEntry() {
    System.out.println("user validate entry!");
}

... 여기 내 견해

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <EditText android:id="@+id/studentEntrySalary" android:text="Foo" android:layout_width="wrap_content" android:layout_height="wrap_content" />
 </LinearLayout>

내 가상 장치에 뭔가 잘못되었을까요?

도움이 되었습니까?

해결책

이렇게해야합니다.

yourEditTextHere.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

                // NOTE: In the author's example, he uses an identifier
                // called searchBar. If setting this code on your EditText
                // then use v.getWindowToken() as a reference to your 
                // EditText is passed into this callback as a TextView

                in.hideSoftInputFromWindow(searchBar
                        .getApplicationWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
               userValidateEntry();
               // Must return true here to consume event
               return true;

            }
            return false;
        }
    });

다른 팁

단일 라인 = "true"를 유지하고 imeoptions = "actiondone"을 edittext에 추가하십시오. 그런 다음 oneDitorActionListener에서 ActionId == editorInfo.ime_action_done이 있는지 확인하십시오 (그러나 구현으로 변경) :

if (actionId == EditorInfo.IME_ACTION_DONE) {

                if ((username.getText().toString().length() > 0)
                        && (password.getText().toString().length() > 0)) {
                    // Perform action on key press
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(username.getWindowToken(),
                            0);
                    doLogin();
                }
            }

텍스트 상자를 한 줄로 만드는 경우 (프로세서를 레이아웃 XML 파일에서 단일 라인이라고 생각합니다) ENTER시 키보드에서 종료됩니다.

여기에 간다 : http://developer.android.com/reference/android/r.stylable.html#textview_singleline

아래 예제에서와 같이 autocompletetextView를 확장하는 사용자 정의 구성 요소를 만듭니다.

public class PortugueseCompleteTextView extends AutoCompleteTextView {
...
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (event != null &&  (event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
        InputMethodManager inputManager =
                (InputMethodManager) getContext().
                        getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(
                this.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    }
    return super.onKeyPreIme(keyCode, event);
}

AlertDialog.builder 에서이 코드를 사용하고 있지만 활동에 사용할 수 있습니다.

편집 텍스트 에이 줄을 추가하십시오.

android:imeOptions="actionDone"'

키보드 완료 버튼 클릭시 해당 편집 텍스트로 이동하려면 다음 편집 ID를 지정할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top