EditText:避免关注setText()

soc*_*qwe 12 android focus android-edittext

我有一个带有滚动视图的活动,其中包含一些按钮和一个EditText,因此按钮位于EditText组件上方.

通过单击Button,EditText组件可以使用更改其文本值 EditText.setText("foo");

不幸的是,此调用将焦点设置为EditText组件,因此ScrollView将滚动到EditText组件(光标闪烁).

有办法避免这种情况吗?

我试图做这样的事情

public void setText(String t){
    editText.setFocusable(false)
    editText.setFocusableInTouchMode(false);
    editText.setText(t);
    editText.setFocusable(true);
    editText.setFocusableInTouchMode(true);
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,有时setFocusable(true)没有影响,因此EditText不再"可点击"了.

Ame*_*eer 0

在您的按钮中使用以下代码

InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
Run Code Online (Sandbox Code Playgroud)

请参考以下链接

android edittext 单击按钮后移除焦点