【问题标题】:Android soft keyboard not pushing up views that are reselectedAndroid软键盘不向上推重新选择的视图
【发布时间】:2017-03-27 10:52:20
【问题描述】:

所以,我有一个EditText,当您在EditText 外部单击时,我有一个隐藏键盘的功能。接下来会发生什么:

  1. 第一次选择EditText,键盘出现并将整个View向上推。
  2. 我取消选择 EditText(单击 EditText 的任何其他位置),键盘进入隐藏状态
  3. 当我再次单击EditText 时,键盘出现,但View 没有拉起,我看不到我的EditText

EditText被选中时,如何再次向上推该视图?

这里是隐藏软键盘的代码:

 @Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    View v = getCurrentFocus();

    if (v != null &&
            (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) &&
            v instanceof EditText &&
            !v.getClass().getName().startsWith("android.webkit.")) {
        int scrcoords[] = new int[2];
        v.getLocationOnScreen(scrcoords);
        float x = ev.getRawX() + v.getLeft() - scrcoords[0];
        float y = ev.getRawY() + v.getTop() - scrcoords[1];

        if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom())
            hideKeyboard(this);
    }
    return super.dispatchTouchEvent(ev);
}

hideKeyboard() 方法

public static void hideKeyboard(Activity activity) {
    if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);

    }
}

【问题讨论】:

标签: android android-edittext keyboard android-input-method android-textinputedittext


【解决方案1】:

使用这个方法:

@Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.
                INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        return true;
    }

【讨论】:

  • @Miljan 成功了,我已经检查过了。你能试试这个吗。
  • EditText EditHello= (EditText) findViewById(R.id.edt_hello); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(EditHello, InputMethodManager.SHOW_IMPLICIT);
【解决方案2】:

找到解决方案...

我不得不使用虚拟布局(在我的例子中是 LinearLayout):

public static void hideKeyboard(Activity activity) {
    if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);

    }    
activity.findViewById(R.id.dummy).requestFocus();    
}

现在,当我重新选择我的 EditText 时,它可以工作了。

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 2011-05-11
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2021-11-28
    • 2013-08-10
    相关资源
    最近更新 更多