日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

EditText设置可以编辑和不可编辑状态

發布時間:2023/12/15 综合教程 29 生活家
生活随笔 收集整理的這篇文章主要介紹了 EditText设置可以编辑和不可编辑状态 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、首先想到在xml中設置android:editable="false",但是如果想在代碼中動態設置可編輯狀態,沒有找到對應的函數

2、然后嘗試使用editText.setFocusable(false);和editText.setEnabled(false);設置不可編輯狀態;editText.setFocusable(true);和editText.setEnabled(true);設置可編輯狀態。

發現在editText.setFocusable(false);和editText.setEnabled(false);時不可編輯,但是editText.setFocusable(true);和editText.setEnabled(true);也是不可編輯的,感覺這個時候EditText控件高亮度了,但是沒有焦點

3、最后嘗試使用editText.setFocusable(false);和editText.setFocusableInTouchMode(false);設置不可編輯狀態;editText.setFocusableInTouchMode(true);editText.setFocusable(true);editText.requestFocus();設置可編輯狀態

這個可以實現可編輯和不可編輯,但是又發現一個問題,在不可編輯狀態如果常按住控件,可以進行粘帖操作,不知道怎么可以不能進行粘帖啊

--------------------------------------------------------

其實可以直接通過setEnable(true)編輯;setEnable(false)編輯

----------------------------------------------------------

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.

總結

以上是生活随笔為你收集整理的EditText设置可以编辑和不可编辑状态的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。