EditText设置可以编辑和不可编辑状态
1、首先想到在xml中設(shè)置android:editable="false",但是如果想在代碼中動態(tài)設(shè)置可編輯狀態(tài),沒有找到對應(yīng)的函數(shù)
2、然后嘗試使用editText.setFocusable(false);和editText.setEnabled(false);設(shè)置不可編輯狀態(tài);editText.setFocusable(true);和editText.setEnabled(true);設(shè)置可編輯狀態(tài)。
發(fā)現(xiàn)在editText.setFocusable(false);和editText.setEnabled(false);時不可編輯,但是editText.setFocusable(true);和editText.setEnabled(true);也是不可編輯的,感覺這個時候EditText控件高亮度了,但是沒有焦點
3、最后嘗試使用editText.setFocusable(false);和editText.setFocusableInTouchMode(false);設(shè)置不可編輯狀態(tài);editText.setFocusableInTouchMode(true);editText.setFocusable(true);editText.requestFocus();設(shè)置可編輯狀態(tài)
這個可以實現(xiàn)可編輯和不可編輯,但是又發(fā)現(xiàn)一個問題,在不可編輯狀態(tài)如果常按住控件,可以進行粘帖操作,不知道怎么可以不能進行粘帖啊
--------------------------------------------------------
其實可以直接通過setEnable(true)編輯;setEnable(false)編輯
----------------------------------------------------------
android:editable is deprecated: Use an <EditText> to make it editable
android:editable is deprecated: Use inputType instead
分析:關(guān)于EditText控件的read-only問題,即: 無法通過UI更改其中的內(nèi)容, 但可以選定部分內(nèi)容, 進行復制.在早期的sdk, EditText有Editable屬性, 現(xiàn)在這個屬性已經(jīng)deprecated了.
解決方法:
其實只需一行代碼就能搞定et.setKeyListener(null);
注意, 這里不是setOnKeyListener, 而是setKeyListener. 此方法是TextView的成員, 調(diào)用后的效果完全符合預期, 并且獲得焦點后不會彈出輸入法.
下面是官方文檔的解釋
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.
總結(jié)
以上是生活随笔為你收集整理的EditText设置可以编辑和不可编辑状态的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美国无人驾驶立法提案将进行投票,有望解除
- 下一篇: Nginx实现404页面的几种方法