1. 程式人生 > >Android設定EditText不可編輯

Android設定EditText不可編輯

EditText editText =  (EditText) findViewById(R.id.editText);

只需一行程式碼:editText.setKeyListener(null);

即可。

官方文件解析:
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.

奮鬥