最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to open keyboard on Android by javascript without click or touch event? - Stack Overflow

programmeradmin0浏览0评论

Is it possible to open keyboard on Android without click or touch event? For example just after appending textarea to some element? element.focus() works for me on iOS but not on Android.

Is it possible to open keyboard on Android without click or touch event? For example just after appending textarea to some element? element.focus() works for me on iOS but not on Android.

Share Improve this question asked Jan 8, 2015 at 8:00 andrewgdaandrewgda 1532 silver badges10 bronze badges 4
  • 2 @Vixed you should edit the question to make it fit better your own issue. As-is it is unclear whether it is for an Android native app in Java (as the current answer assumes), or for an hybrid app in JavaScript, e.g. using Cordova. – ghybs Commented Oct 27, 2017 at 8:04
  • Please clarify does your app is in java or an hybrid app. – Jaiprakash Soni Commented Oct 27, 2017 at 11:09
  • Webview javascript (jQuery) @ghybs – Vixed Commented Oct 27, 2017 at 14:57
  • check this stackoverflow./questions/9703271/… – Jaiprakash Soni Commented Oct 30, 2017 at 4:54
Add a ment  | 

4 Answers 4

Reset to default 1

Simply add "requestFocus" to your XML. Something like

        <EditText
        android:id="@+id/editText"
        ... />
        <requestFocus />

and in onCreate()

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

Or: It could be just

editText.requestFocus();
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

This answer is based on the following assumption: You are using a WebView in your android app to insert a custom Javascript to achieve a certain task

Firstly create a method in your Android java class that will do do the job of popping up the keyboard:

@JavascriptInterface
public void takeUserInput() {
     mWebView.setFocusable(true);
     mWebView.setFocusableInTouchMode(true);
}

In your Javascript make a call to the takeUserInput() method whenever you want to pop-up the keyboard.

You can read up on how to call android functions from Javascript.

Hope this helps!

You have to make sure that the window's soft input mode is set to "always visible" before requesting focus on your element. You can set it using:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

After that you can bring up the keyboard by requesting focus on your element:

element.requestFocus();

According to my Knowledge you can't open the keyboard without using a input. Try to use virtual keyboard read following.

Show virtual keyboard on mobile phones in javascript

发布评论

评论列表(0)

  1. 暂无评论