How to correctly handle Android EditText input inside a ListView?

浪尽此生 提交于 2019-12-09 13:44:28

问题


In my application I have an activity to add/remove/edit records inside a SortedMap. The activity is implemented as an extension of ListActivity. I have implemented custom ArrayAdapter for the collection items.

Every ListView item (which corresponds to an underlying record) consists of TextViews, EditTexts, and a Button to delete the record itself. The layout is roughly as follows:

ListView
----------------------------------------------------
[TextView] [EditText] [TextView] [EditText] [Button]
----------------------------------------------------
[TextView] [EditText] [TextView] [EditText] [Button]

My goal is to process the input a user types to the EditTexts as soon as the user finished editing, i.e. when the user has navigate away from the EditText or the user has pressed back to dismiss the onscreen keyboard.

I have tried implementing this by handling onFocusChanged, to process the text visible in the EditText. However this method is not working well, onFocusChanged method is called very often and randomly, even for unselected & unedited EditTexts. This is probably due to this article on Android Blog from this StackOverflow question.

Is there a better way to do this?


回答1:


TextView, which EditText extends, includes the method public void addTextChangedListener (TextWatcher watcher) that may be useful to you.

I have not used it before but it seems like this would be called anytime the user edits the TextView and then you could process it every time it changes. This may cause a lot of overhead if it is called every time a user deletes or adds a character, but it may be the easiest solution.



来源:https://stackoverflow.com/questions/3849197/how-to-correctly-handle-android-edittext-input-inside-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!