我试图让我的用户重命名一个文件使用应用程序,我的问题是更多的设计。我希望在重命名时,EditText将包括旧的名称,并且它将被选中,而不包括文件扩展名。
我成功地做到了这一点,但我的问题是,即使选择了文本,键盘和文本上的光标也没有显示。这使得用户点击editText来重命名它,从而取消了选择,所以这就是为什么它真正困扰我的原因。
供参考的图像:

我的EditText xml (忽略可见性属性):
<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);
}
}
}
});有什么建议吗?
发布于 2015-10-24 16:39:41
一旦设置了selectAll()或setSelection(),就可以弹出键盘。
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(renameEdit.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);发布于 2015-10-24 16:40:38
有几种方法可以编程打开软键盘。
甚至有几种在清单中以声明方式进行操作的方法;但是,这需要在活动启动时打开键盘。
试试这个.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
其他可能的答案在这里- Open soft keyboard programmatically
https://stackoverflow.com/questions/33320560
复制相似问题