Android的EditText无法打开键盘

时间:2015-10-24 16:32:50

标签: android android-edittext

我正在尝试让我的用户使用应用程序重命名文件,我的问题更多的是关于设计。我希望在重命名时,EditText将包含旧名称,它将被选中,不包括文件扩展名。
我设法做到了,但我的问题是,即使文本被选中,键盘和文本上的光标也没有显示。这使用户点击editText重命名它,这取消了选择,所以这就是为什么它真的困扰我。
图片供参考:
Image for reference
我的EditText xml(忽略visibility属性):

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/renameEditText"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:layout_marginBottom="8dp"
        android:paddingLeft="20dp"
        android:visibility="gone"
        android:focusable="true"/>

我的设置选择代码:

renameEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    String text = renameEdit.getText().toString();
                    if (!text.isEmpty()) {
                        int index = text.lastIndexOf('.');
                        if (index == -1)
                            renameEdit.selectAll();
                        else
                            renameEdit.setSelection(0, index);
                    }
                }
            }
        });

有任何建议吗?

2 个答案:

答案 0 :(得分:3)

有几种方法可以通过编程方式打开软键盘。

甚至在清单中以声明方式执行此操作的几种方式;但是,这需要在活动开始时打开键盘。

试试这个......

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

其他可能的答案在这里 - Open soft keyboard programmatically

答案 1 :(得分:1)

设置selectAll()setSelection()后,您可以弹出键盘。

    InputMethodManager inputMethodManager =  (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInputFromWindow(renameEdit.getApplicationWindowToken(),     InputMethodManager.SHOW_FORCED, 0);
相关问题