在对话活动上自动显示软键盘

时间:2010-12-05 21:21:14

标签: java android keyboard dialog android-activity

我有一个简单的活动,在清单中使用android:theme="@android:style/Theme.Dialog"

我的活动包括EditText,2个按钮和TextView。它只不过是一个用户输入名称的框,然后按确定/取消。

我只想关注EditText,并在Activity启动时自动显示软键盘。我已经阅读了无数关于此的帖子,但我似乎无法让它工作。当活动开始时,EditText中会出现闪烁的光标,但是在我点击它之前键盘才会显示。

这是我的活动:

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

我也在onCreate中试过这个:

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

编辑:我的清单供参考

    <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>

2 个答案:

答案 0 :(得分:2)

以下内容应该有效。转到您的清单并使用android:windowSoftInputMode属性更新您的活动行。

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

有关可以传递到此属性的不同参数的详细信息,请参阅以下documentation页面。

我测试了以上内容,它对我来说很好。这是我非常简单的例子。 代码:

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

布局:

<?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>

清单:

    <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>

答案 1 :(得分:1)

试试这个为我工作

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                   imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
相关问题