问题描述
我试图让我的用户使用该应用程序重命名文件,我的问题更多是关于设计。
我希望重命名时,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);
}
}
}
});
有什么建议吗?
1楼
有几种方法可以通过编程方式打开软键盘。
清单中甚至有几种声明式的方式; 但是,这需要在活动开始时打开键盘。
尝试这个...
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
其他可能的答案在这里-以
2楼
一旦设置了selectAll()
或setSelection()
,就可以弹出键盘。
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(renameEdit.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);