编辑文本焦点问题

时间:2016-10-19 07:56:03

标签: android android-edittext focus

我有一个活动,在一个活动中编辑一个带有一些提示文本的文本。

当我默认启动活动时,焦点会转到编辑文本并提示不可见。

请告诉我如何使提示可见

谢谢

3 个答案:

答案 0 :(得分:1)

在页面中添加一个空视图:

<LinearLayout
  android:focusable="true" 
  android:focusableInTouchMode="true"
  android:layout_width = "0dp"
  android:layout_height = "0dp"/>

此视图将获得焦点并阻止EditText获取

答案 1 :(得分:1)

在setContentView()

之前在onCreate中添加此行
dat

如果你在xml部分有requestFocus,那么删除它

答案 2 :(得分:0)

在setContentView()之后:

KeyListener keyListener = new TextKeyListener(TextKeyListener.Capitalize.CHARACTERS, false);
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
setInput(YOUR_EDIT_TEXT, keyListener, imm);

setInput方法:

private void setInput(EditText _editText, KeyListener keyListener, InputMethodManager imm){
_editText.setKeyListener(keyListener);
if (_editText.isFocusable()) {
    _editText.setFocusable(true);
    _editText.setFocusableInTouchMode(true);
    _editText.requestFocus();
}

_editText.setSelection(_editText.getText().length());
imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(_editText, InputMethodManager.SHOW_IMPLICIT);
}
相关问题