Show soft keyboard when Activity starts

风格不统一 提交于 2019-12-17 22:17:30

问题


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?


回答1:


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

android:windowSoftInputMode="stateVisible"

Hope that helps for you as well.




回答2:


Easiest solution: Put

android:windowSoftInputMode = "stateVisible" 

in Activity section of AndroidManifest.xml




回答3:


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.




回答4:


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

android:windowSoftInputMode="stateVisible|adjustPan"



回答5:


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);
}



回答6:


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




回答7:


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"



回答8:


File : AndroidManifest.xml

<activity android:name=".MainActivity">

Add following property :

android:windowSoftInputMode="stateVisible"

Which worked for me.




回答9:


paste this after setContentView

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


来源:https://stackoverflow.com/questions/2466516/show-soft-keyboard-when-activity-starts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!