2013-10-17 30 views
8

我有一個scrollview包含一些按鈕和EditText的活動所以這些按鈕位於EditText組件的上方。EditText:避免讓注意力集中在setText()

通過點擊一個按鈕的EditText組件使用EditText.setText("foo");

不幸的是這調用將焦點設置到組件的EditText改變他的文本值,所以滾動型將滾動到的EditText組件(光標閃爍)。

有沒有辦法避免這種情況?

我試圖做這樣的事情

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

但是,這並不工作,有時setFocusable(true)沒有任何影響,所以EditText上不是「點擊」了。

+0

嘗試在xml中更改EditText的可聚焦性。 –

+0

但它應該仍然可編輯 – sockeqwe

+0

嘗試將textwatcher偵聽器設置爲null,然後檢查。 –

回答

0

在下面的代碼你的按鈕使用

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

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

請參考以下鏈接

android edittext remove focus after clicking a button

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);