问题
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