How to prevent the virtual keyboard from opening Nativescript (IOS/Android)

320 Views Asked by At

I want to create smth like this

enter image description here

But the native keyboard always opens

The result should be like this: TextView has focus but no virtual keyboard

I can add editable="false" attr but after this i lose cursor func I would like to keep the basic functionality of the input field

1

There are 1 best solutions below

0
On

You can attach to the loaded event and try to dismiss soft input, maybe after a little timeout.

import { isAndroid, Utils } from "@nativescript/core";

// ...

async onLoaded() {
  setTimeout(() => {
    if (isAndroid) {
      Utils.android.dismissSoftInput();
    } else {
      UIApplication.sharedApplication.keyWindow.endEditing(true);
    }
  }, 100);
}

On Android you can also move the focus to another element, by listening to its loaded event:

onLoaded(args: EventData) {
  const view = args.object as LayoutBase;
  if (view.android) {
    view.android.setFocusableInTouchMode(true);
    view.android.setFocusable(true);
  }
}