android设置隐藏键盘按回车键(在EditText中) [英] android set hidden the keyboard on press enter (in a EditText)

查看:665
本文介绍了android设置隐藏键盘按回车键(在EditText中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的用户在虚拟android用户验证项"上按 Enter 时.键盘我的键盘保持可见! (为什么?)

When my user press Enter on the virtual android "user validate entry!" keyboard my keyboard stay visible! (Why?)

这是我的Java代码...

Here my Java code...

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>

我的虚拟设备上可能有问题吗?

Maybe something wrong on my virtual device?

推荐答案

这应该做到:

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

这篇关于android设置隐藏键盘按回车键(在EditText中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆