ChatGPT解决这个技术问题 Extra ChatGPT

Show soft keyboard when Activity starts

I have 2 activities, A and B. When A starts, it checks for a condition and if true, it calls startActivityForResult() to start B. B only takes text input so it makes sense for the soft keyboard to automatically pop up when B start. When the activity starts, the EditText already has focus and it ready for input.

The problem is that the keyboard never shows up, even with windowSoftInputMode="stateAlwaysVisible" set in the manifest under the <activity> tag for B. I also tried with the value set to stateVisible. Since it doesn't show up automatically, I have to tap the EditText to make it show.

Anyone know what the solution might be?

(Shooting in the dark, but) have you tried requestFocus on the text box?

L
Leo

What worked best for me is in Android Manifest for activity B adding

android:windowSoftInputMode="stateVisible"

Hope that helps for you as well.


I should note that so far, this works well for the Nexus One running 2.2 but some of the Motorola devices such as Milestone with 2.1 seem to ignore this.
Thanks, to do that programmatically check stackoverflow.com/questions/5593053/…
Using stateVisible results in the keyboard reappearing when the device orientation changes. If this behaviour is not desired, then stateVisible is not the solution.
For newbies like me: you must add this line right after
The problem with this method is that it can't be propagated to many apps using a central library. You need to repeat this behavior in all manifest files. A programmatic solution solves that problem too.
U
Uncaught Exception

Easiest solution: Put

android:windowSoftInputMode = "stateVisible" 

in Activity section of AndroidManifest.xml


For newbies like me: you must add this line right after
s
synic

If requestFocus on an EditText isn't showing it, maybe this'll do it:

InputMethodManager imm = (InputMethodManager)getSystemService(
    Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, 0);

Look here for more information.


this should work so long as you're not specifying a different soft input state anywhere else, like in your manifest or in code. i.e this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
P
Paul

For me worked only this solutions: add in manifest for that activity:

android:windowSoftInputMode="stateVisible|adjustPan"

Did it work for devices with hardware keyboard? I'm having a hard time working this in hardware keyboard devices i.e 2.2 devices
For newbies like me: you must add this line right after
a
abc

I have got two way.

Method 1. Use the following code inside the OnCreate method

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

It will prevent popping up keyboard unless you click.

or

Method 2 You can move away the focus on other view like TextView by using "requestfocus" in the xml.

<TextView
            android:id="@+id/year_birth_day"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1991">            
           <requestFocus />
           </TextView>

Method 3 ( I think it should be avoidable) Using the following code in the manifest-

android:windowSoftInputMode="stateVisible"

using is the only thing that worked for me. Thank you
w
whlk

Try showing the keyboard with some delay. Something similar to this:

public void onResume() {
    super.onResume();

    TimerTask tt = new TimerTask() {

        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(yourTextBox, InputMethodManager.SHOW_IMPLICIT);
        }
    };

    final Timer timer = new Timer();
    timer.schedule(tt, 200);
}

timer task? for what?
@busylee you can use a Handler or any other method of executing something in a delay of your choosing. This is only an example.
There is no reason to use delay
K
Kishan Solanki

Major Attention Required!

android:windowSoftInputMode="stateVisible|adjustPan" This alone won't work to show keyboard on activity start.

You also need to explicitly add this into your class

editTextXYZ.requestFocus()
        val imm: InputMethodManager =
            getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(editTextXYZ, InputMethodManager.SHOW_IMPLICIT)

C
Chad Hedgcock

If you're using an emulator, you have to turn the hard keyboard off in order for the soft keyboard to show.


P
Paul Roub

File : AndroidManifest.xml

<activity android:name=".MainActivity">

Add following property :

android:windowSoftInputMode="stateVisible"

Which worked for me.


A
Ashwini

paste this after setContentView

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