我正在尝试让我的用户使用该应用程序重命名文件,但我的问题更多地与设计有关。我希望在重命名时,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);
}
}
}
});
有什么建议吗?
请您参考如下方法:
有几种方法可以通过编程方式打开软键盘。
甚至在 list 中以声明方式执行此操作的几种方法;但是,这需要在 Activity 开始时打开键盘。
试试这个...
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
其他可能的答案在这里 - Open soft keyboard programmatically