我在Android中有一个活动,包含两个元素:
编辑文本列表视图
当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:
EditText.setSelected(false);
EditText.setFocusable(false);
没有运气。如何说服EditText在“活动”启动时不选择自己?
我在Android中有一个活动,包含两个元素:
编辑文本列表视图
当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:
EditText.setSelected(false);
EditText.setFocusable(false);
没有运气。如何说服EditText在“活动”启动时不选择自己?
当前回答
隐藏键盘的最简单方法是使用setSoftInputMode
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
或者可以使用InputMethodManager并像这样隐藏键盘。
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
其他回答
我需要以编程方式明确关注所有领域。我刚刚在主布局定义中添加了以下两条语句。
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
就这样。立刻解决了我的问题。谢谢,西尔弗,你给我指明了正确的方向。
只需在EditText的父布局中添加android:focuableInTouchMode=“true”,您就可以摆脱这种尴尬的行为。
您有编辑文本和列表。在OnStart/On Create中,应该将焦点设置在listview:listview.requestfocus()上
/**
* set focus to top level window
* disposes descendant focus
* disposes softInput
* */
public static void topLevelFocus(Context context){
if(Activity.class.isAssignableFrom(context.getClass())){
ViewGroup tlView = (ViewGroup) ((Activity) context).getWindow().getDecorView();
if(tlView!=null){
tlView.setFocusable(true);
tlView.setFocusableInTouchMode(true);
tlView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
}
}
}
当按下按钮时,我使用以下代码阻止EditText窃取焦点。
addButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
View focused = internalWrapper.getFocusedChild();
focused.setVisibility(GONE);
v.requestFocus();
addPanel();
focused.setVisibility(VISIBLE);
}
});
Basically, hide the edit text and then show it again. This works for me as the EditText is **not** in view so it doesn't matter whether it is showing.
You could try hiding and showing it in succession to see if that helps it lose focus.