How to hide keyboard after touch outside the textfield?

青春壹個敷衍的年華 提交于 2020-05-30 07:50:07

问题


I have a cross-platform app. I have a form that has more than one TextField. I want to hide the keyboard when the user touch outside of the textfield because it covers the button that sends the data.

How can I do?

In my .html file I have:

<ScrollView>
  <GridLayout ios:style="margin-top:50">
    <StackLayout class="form">

      <!-- Some TextView -->

    </StackLayout>
  </GridLayout>
</ScrollView>

EDIT

This is a Playground that show the error.


回答1:


Add a tap listener to your layout and hide keyboard using

iOS

import * as utils from "tns-core-modules/utils/utils";

UIApplication.sharedApplication
        .keyWindow
        .endEditing(true);

Android

utils.ad.dismissSoftInput();

Edit

You may simply call dismissSoftInput() method on the TextField if it's just one TextField in your Page. The above code helps if you have multiple TextFields on your Page and not sure which one is actually focused.

Playground Sample




回答2:


The correct way to hide the keyboard in the latest version of NS 6.0+ is to directly access the context.

import { isIOS, isAndroid } from 'tns-core-modules/platform';
import * as utils from 'tns-core-modules/utils/utils';
declare const UIApplication;

  dismissKeyboard() {
    if (isIOS) {
      UIApplication.sharedApplication.keyWindow.endEditing(true);
    }
    if (isAndroid) {
      utils.ad.dismissSoftInput();
    }
  }



回答3:


In Android hide keyboard with:

public void hideSoftKeyboard(Activity mActivity) {
    InputMethodManager inputManager = (InputMethodManager) mActivity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(mActivity.getCurrentFocus().getWindowToken(), 0);
}



回答4:


Kotlin:

yourTextView.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus -> if (!hasFocus) {
 //hide Keyboard
  }
}

Also make add to the parent layout 'focusableInTouchMode = true' in the XML



来源:https://stackoverflow.com/questions/56043761/how-to-hide-keyboard-after-touch-outside-the-textfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!