简体   繁体   中英

Soft Keyboard doesn't pop up automatically

Layout is declared like this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.guna.testapplication.MainActivity">

    <EditText
        android:hint="@string/action_settings"
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <requestFocus />
    </EditText>
</RelativeLayout>

Activity declared like this.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

And Activity added in manifest as like this.

<activity
    android:name=".MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

When I run this app I am getting following output.

在此处输入图片说明

I don't know why soft keyboard doesn't pop up automatically.

To force the soft keyboard to appear, you can use

 EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

Add this to your edittext and try

android:focusable="true"
android:focusableInTouchMode="true"
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name"
 android:windowSoftInputMode="adjustPan|adjustNothing">
 <intent-filter>
    <action android:name="android.intent.action.MAIN" />       
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

or in your activity

EditText actionSettings= (EditText) findViewById(R.id.action_settings);
InputMethodManager imm = (InputMethodManager) 
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(actionSettings, InputMethodManager.SHOW_IMPLICIT);

将此行添加到清单活动中。

android:windowSoftInputMode="stateAlwaysVisible"

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