1

当我的活动开始时,我的 edittext 会自动获得焦点,但不会出现软键盘。此外,当我以编程方式调用.requestFocus()视图时,它会获得焦点,但软键盘再次没有出现。一个视图示例如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp">

 <EditText
    android:id="@+id/editTextTransactionName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/edittext_description"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:imeOptions="actionNext"
    android:inputType="text"/> 
</LinearLayout>

软键盘似乎只有在我单击时才会出现。这是否有理由成为默认行为?打开用作表单的活动时,我直观地希望键盘出现,以便您可以立即输入数据。

4

1 回答 1

2

考虑阅读以下内容: Android 操作栏选项卡和键盘焦点

requestFocus 非常不可靠。

显然这不是默认行为。如果您真的希望键盘自动出现,请在您的 中模拟“点击” EditText,这对我有用(这比调用更安全,showSoftInput因为 的行为不可靠requestFocus,而且您不需要对键盘进行微观管理):

EditText tv = (EditText)findViewById(R.id.editText);
tv.post(new Runnable() {

            @Override
            public void run() {
                Log.d("RUN", "requesting focus in runnable");
                tv.requestFocusFromTouch();
                tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , tv.getWidth(), tv.getHeight(), 0));
                tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , tv.getWidth(), tv.getHeight(), 0));
            }
        });

我认为键盘没有打开的原因是用户必须有机会在决定首先从哪里开始编辑之前看到整个窗口。

于 2012-09-04T19:38:42.707 回答