让EditText 不可编辑

如果让EditText不可编辑在xml实现很简单android:editable 可是在代码中却没有setEditable

只能使用

editText.setFilters(new InputFilter[] { new InputFilter() {
 @Override
 public CharSequence filter(CharSequence source, int start,
  int end, Spanned dest, int dstart, int dend) {
   return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
 }
} });

 

Java代码  复制代码  收藏代码
  1. import android.app.Activity;   
  2. import android.os.Bundle;   
  3. import android.text.InputFilter;   
  4. import android.text.Spanned;   
  5. import android.view.View;   
  6. import android.widget.Button;   
  7. import android.widget.EditText;   
  8.   
  9. /**  
  10.  * Class which shows how to lock and unlock EditText component  
  11.  *  
  12.  * @author FaYnaSoft Labs  
  13.  */  
  14. public class Main extends Activity {   
  15.     private EditText editText;   
  16.     private boolean value = false;   
  17.   
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {   
  20.         super.onCreate(savedInstanceState);   
  21.         setContentView(R.layout.main);   
  22.         editText = (EditText) findViewById(R.id.textId);   
  23.         editText.setText("EditText component");   
  24.         Button b = (Button) findViewById(R.id.btnId);   
  25.         b.setText("Lock/Unlock");   
  26.         b.setOnClickListener(new View.OnClickListener() {   
  27.   
  28.             @Override  
  29.             public void onClick(View v) {   
  30.                 if (value) {   
  31.                     value = false;   
  32.                 } else {   
  33.                     value = true;   
  34.                 }   
  35.                 lockUnlock(value);   
  36.             }   
  37.         });   
  38.     }   
  39.   
  40.     /**  
  41.      * Method which locks and unlocks editText component  
  42.      * @param value our boolean value which using in or if operator  
  43.      */  
  44.     private void lockUnlock(boolean value) {   
  45.         if (value) {   
  46.             editText.setFilters(new InputFilter[] { new InputFilter() {   
  47.                 @Override  
  48.                 public CharSequence filter(CharSequence source, int start,   
  49.                         int end, Spanned dest, int dstart, int dend) {   
  50.                     return source.length() < 1 ? dest.subSequence(dstart, dend)   
  51.                             : "";   
  52.                 }   
  53.             } });   
  54.         } else {   
  55.             editText.setFilters(new InputFilter[] { new InputFilter() {   
  56.                 @Override  
  57.                 public CharSequence filter(CharSequence source, int start,   
  58.                         int end, Spanned dest, int dstart, int dend) {   
  59.                     return null;   
  60.                 }   
  61.             } });   
  62.         }   
  63.     }   
  64. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值