尝试在活动启动时弹出Android键盘

时间:2012-07-29 16:59:34

标签: android xml android-layout android-keypad

我希望Android键盘在我的活动启动时弹出。一个简单的谷歌搜索显示你只需requestFocus我在我的.xml中,但它仍然没有弹出。我是否犯了任何导致这种情况无效的轻微错误?

测试:

物理4.1 模拟器2.2

layout.xml:

<EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="To:"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

2 个答案:

答案 0 :(得分:2)

这有效:

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

答案 1 :(得分:0)

试试这段代码:

EditText input = (EditText) findViewById(R.id.editText1);
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);         
inputManager.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);