EditText:避免在setText()上获取焦点

14

我有一个包含一些按钮和EditText的ScrollView活动,所以这些按钮在EditText组件之上。

通过点击一个按钮,可以使用EditText.setText("foo");更改EditText组件的文本值。

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

有没有办法避免这种情况?

我已经尝试过像这样做:

public void setText(String t){
    editText.setFocusable(false)
    editText.setFocusableInTouchMode(false);
    editText.setText(t);
    editText.setFocusable(true);
    editText.setFocusableInTouchMode(true);
}

但这并不起作用,有时 setFocusable(true) 没有影响,因此 EditText 不再“可点击”。


尝试在 XML 中更改 EditText 的焦点能力。 - Jitender Dev
但它应该仍然可编辑。 - sockeqwe
尝试将TextWatcher监听器设置为null,然后检查。 - Pankaj Kumar
请检查我的修改后的答案。 - Harshit Rathi
4个回答

2

这是该问题最恰当的解决方案。 - Nguyễn Minh Vũ

0

在你的xml中使用这个:

<EditText
        android:clickable="false" 
        android:cursorVisible="false" 
        android:focusable="false" 
        android:focusableInTouchMode="false">
</EditText>

或者

edittext.setKeyListener(null);

0
在你的按钮中使用以下代码。
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);

请参考下面的链接:

android edittext 点击按钮后移除焦点


0
在你的按钮中使用以下代码......
 InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 

 inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接