Android 的EditText實現不可編輯


android:editable is deprecated: Use an <EditText> to make it editable

android:editable is deprecated: Use inputType instead

分析:關於EditText控件的read-only問題,即: 無法通過UI更改其中的內容, 但可以選定部分內容, 進行復制.在早期的sdk, EditText有Editable屬性, 現在這個屬性已經deprecated了.
 
解決方法:
其實只需一行代碼就能搞定et.setKeyListener(null);
注意, 這里不是setOnKeyListener, 而是setKeyListener. 此方法是TextView的成員, 調用后的效果完全符合預期, 並且獲得焦點后不會彈出輸入法. 
 
下面是官方文檔的解釋
Sets the key listener to be used with this TextView. This can be null to disallow user input. Note that this method has significant and subtle interactions with soft keyboards and other input method: see KeyListener.getContentType() for important details. Calling this method will replace the current content type of the text view with the content type returned by the key listener.
 
Be warned that if you want a TextView with a key listener or movement method not to be focusable, or if you want a TextView without a key listener or movement method to be focusable, you must call setFocusable again after calling this to get the focusability back the way you want it.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM