开发者

Display soft keyboard automatically on a dialog activity

I have a simple Activity that uses a android:theme="@android:style/Theme.Dialog" in the manifest.

My activity consists of an EditText, 2 Buttons, and a TextView. It is nothing mo开发者_StackOverflow社区re than a box for the user to enter in a name and press OK/Cancel.

I just want to focus the EditText and have the soft keyboard automatically show when the Activity is started. I've read countless posts about this but I just can't seem to get it to work. When the activity starts the blinking cursor appears in the EditText, but the keyboard won't show until I click inside it.

Here is my Activity:

public class Finalize extends Activity {

    private EditText mEditName;

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

        mEditName = (EditText) findViewById(R.id.file_name_edit);
        mEditName.setFocusable(true);
        mEditName.requestFocus();

        mEditName.setOnFocusChangeListener(new View.OnFocusChangeListener() {           
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        });
    }
}

I've also tried this in onCreate:

    InputMethodManager mgr = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(mEditName, 0);

Edit: My manifest for reference

    <activity class=".Finalize"
        android:name=".Finalize"
        android:label="@string/file_name_title"
        android:theme="@android:style/Theme.Dialog"
        android:screenOrientation="portrait"        
        android:windowSoftInputMode="stateAlwaysVisible">
    </activity>


The following should work. Go to your manifest and update your activity line with the android:windowSoftInputMode attribute.

<activity android:name=".Finalize"
    android:windowSoftInputMode="stateAlwaysVisible">
    ...
</activity>

See the following documentation page for more details on the different parameters that can be passed into this attribute.

I tested the above and it works fine for me. Here is my terribly simple example. Code:

public class DialogActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
    }
}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello"    />
    <EditText android:id="@+id/edit_text_test"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"/>
</LinearLayout>

Manifest:

    <activity android:name=".DialogActivity"
              android:windowSoftInputMode="stateAlwaysVisible"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.Dialog">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


Try this its work for me

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                   imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜