简体   繁体   中英

Show software keyboard without EditText

My goal is to show/hide on-screen software keyboard on some event and intercept input from that keyboard.

I found out that soft keyboard can be shown for some View class descendants, but I don't need any visual representation of the text edit widget on screen, just the ability to programmatically show/hide soft keyboard with input interception.

What is the best way to achieve this?

Even if this question was asked almost a year ago it didn't have an accepted and fully helpful answer and since I ran into the same problem myself I though I'd share my solution:

As Vikram pointed out this is the way to show the soft input:

InputMethodManager im = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(myView, InputMethodManager.SHOW_FORCED);

BUT you must also set your view as focusable and focusable in touch mode:

myView.setFocusable(true);
myView.setFocusableInTouchMode(true);

or in your view XML:

android:focusable = "true"
android:focusableInTouchMode = "true"

You can force the Softkeyboard to be shown by using:

InputMethodManager im = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(myView, InputMethodManager.SHOW_FORCED);

and to hide:

((InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(findViewById(R.id.YOUR_VIEW).getWindowToken(), 0);

Actually, you can always show a soft keyboard input from manifest. add this line to each activity you want to show soft keyboard:

android:windowSoftInputMode="stateAlwaysVisible"

Soft keyboard will show up no matter if there's no edittext in the view. example:

<activity android:name=".ChatActivity"
    android:windowSoftInputMode="stateAlwaysVisible">

</activity>

But what about getting the input typed by user?

You can use EditText as your view parameter, but you don't have to show that to user, instead, create a parent view (such as LinearLayout ) for your editText view and setVisibility as gone of the parent view. Further, you can keep on getting the input by using the EditText.addTextChangedListener

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone">
<EditText
    android:layout_width="100dp"
    android:layout_height="20dp"
    android:focusable = "true"
    android:focusableInTouchMode = "true"
    android:id="@+id/edt"/>
</LinearLayout>

I had to combine multiple previous answers for this to work

In the activity file, I added this piece of code:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <EditText
        android:id="@+id/edt"
        android:layout_width="100dp"
        android:layout_height="20dp"
        android:focusable="true"
        android:focusableInTouchMode="true" />
</LinearLayout>

Assuming that I want the keyboard to pop up on click of a button, I added this piece of code in the activity (kotlin) file:

binding.button.setOnClickListener {
        binding.edt.requestFocus()
        val im: InputMethodManager =
            (this@MainActivity).getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        im.showSoftInput(binding.edt, InputMethodManager.SHOW_FORCED)
    }

The code worked only after I added the line: binding.edt.requestFocus()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM