Android 禁止Edittext弹出系统软键盘 的几种方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_27672523/article/details/56839837
第一种方法:在XML文件下添加:
android:focusable="true"   
android:focusableInTouchMode="true"

第二种方法:直接关闭输入法

在onCreate中加上:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

第三中方法:在Edittext中设置
.setInputType(InputType.TYPE_NULL);

但是会实现效果,但是会导致光标也无法显示,如果想显示光标,你就需要定义一个方法,在方法内 setInputType(禁止)
public void disableShowInput(){
if (android.os.Build.VERSION.SDK_INT <= 10){
editText.setInputType(InputType.TYPE_NULL);
}else {
Class<EditText> cls = EditText.class;
Method method;
try {
method = cls.getMethod("setShowSoftInputOnFocus",boolean.class);
method.setAccessible(true);
method.invoke(editText,false)
}catch (Exception e) {//TODO: handle exception
}
try {
method = cls.getMethod("setSoftInputShownOnFocus",boolean.class);
method.setAccessible(true);
method.invoke(editText,false);
}catch (Exception e) {//TODO: handle exception
} } }


这时 你会发现,光标是出来了 但是输入文字,光标永远在前面,怎么办,解决思路,监听文字文本内容变化监听方法,在 afterTextChanged文字输入完后,把光标移到最后 .setSelection(Edittext,Edittext.length());看代码:
Edittext.addTextChangedListener(watcher);
private TextWatcher watcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
Edittext.setSelection(Edittext,Edittext.length());
}
};


附加:重写Edittext 在输入框内增加删除文字按钮,就是在Android系统的输入框右边加入一个小图标,点击小图标可以清除输入框里面的内容具体看代码:
  1. import android.content.Context;  
  2. import android.graphics.drawable.Drawable;  
  3. import android.text.Editable;  
  4. import android.text.TextWatcher;  
  5. import android.util.AttributeSet;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.View.OnFocusChangeListener;  
  9. import android.view.animation.Animation;  
  10. import android.view.animation.CycleInterpolator;  
  11. import android.view.animation.TranslateAnimation;  
  12. import android.widget.EditText;  
  13.   
  14. public class RMEditText extends EditText implements    
  15.         OnFocusChangeListener, TextWatcher {   
  16.     /** 
  17.      * 删除按钮的引用 
  18.      */  
  19.     private Drawable mClearDrawable;   
  20.     /** 
  21.      * 控件是否有焦点 
  22.      */  
  23.     private boolean falg;  
  24.    
  25.     public ClearEditText(Context context) {   
  26.         this(context, null);   
  27.     }   
  28.    
  29.     public ClearEditText(Context context, AttributeSet attrs) {   
  30.         //这里构造方法也很重要,不加这个很多属性不能再XML里面定义  
  31.         this(context, attrs, android.R.attr.editTextStyle);   
  32.     }   
  33.       
  34.     public ClearEditText(Context context, AttributeSet attrs, int defStyle) {  
  35.         super(context, attrs, defStyle);  
  36.         init();  
  37.     }  
  38.     private void init() {   
  39.         //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片  
  40.         mClearDrawable = getCompoundDrawables()[2];   
  41.         if (mClearDrawable == null) {   
  42. //          throw new NullPointerException("You can add drawableRight attribute in XML");  
  43.             mClearDrawable = getResources().getDrawable(R.drawable.delete_selector);   
  44.         }     
  45.         mClearDrawable.setBounds(00, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());   
  46.         //默认设置隐藏图标  
  47.         setClearIconVisible(false);   
  48.         //设置焦点改变的监听  
  49.         setOnFocusChangeListener(this);   
  50.         //设置输入框里面内容发生改变的监听  
  51.         addTextChangedListener(this);   
  52.     }   
  53.     /** 
  54.      * 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件 
  55.      * 当我们按下的位置 在  EditText的宽度 - 图标到控件右边的间距 - 图标的宽度  和 
  56.      * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向就没有考虑 
  57.      */  
  58.     @Override   
  59.     public boolean onTouchEvent(MotionEvent event) {  
  60.         if (event.getAction() == MotionEvent.ACTION_UP) {  
  61.             if (getCompoundDrawables()[2] != null) {  
  62.   
  63.                 boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())  
  64.                         && (event.getX() < ((getWidth() - getPaddingRight())));  
  65.                   
  66.                 if (touchable) {  
  67.                     this.setText("");  
  68.                 }  
  69.             }  
  70.         }  
  71.         return super.onTouchEvent(event);  
  72.     }  
  73.     /** 
  74.      * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏 
  75.      */  
  76.     @Override   
  77.     public void onFocusChange(View v, boolean hasFocus) {   
  78.         this.falg = hasFocus;  
  79.         if (hasFocus) {   
  80.             setClearIconVisible(getText().length() > 0);   
  81.         } else {   
  82.             setClearIconVisible(false);   
  83.         }   
  84.     }   
  85.     /** 
  86.      * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去 
  87.      * @param visible 
  88.      */  
  89.     protected void setClearIconVisible(boolean visible) {   
  90.         Drawable right = visible ? mClearDrawable : null;   
  91.         setCompoundDrawables(getCompoundDrawables()[0],   
  92.                 getCompoundDrawables()[1], right, getCompoundDrawables()[3]);   
  93.     }   
  94.     /** 
  95.      * 当输入框里面内容发生变化的时候回调的方法 
  96.      */  
  97.     @Override   
  98.     public void onTextChanged(CharSequence s, int start, int count,   
  99.             int after) {   
  100.                 if(falg){  
  101.                     setClearIconVisible(s.length() > 0);  
  102.                 }  
  103.     }   
  104.     @Override   
  105.     public void beforeTextChanged(CharSequence s, int start, int count,   
  106.             int after) {      
  107.     }   
  108.     @Override   
  109.     public void afterTextChanged(Editable s) {      
  110.     }     
  111. }  
在xml文件中这样用
  1.  <com.example.edittext.RMEditText  
  2.         android:hint="输入文字"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="wrap_content"  >  
  5.     </> 
  6. 1

  • setClearIconVisible()方法,设置隐藏和显示清除图标的方法,调用setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)来设置上下左右的图标

  • setOnFocusChangeListener(this) 为输入框设置焦点改变监听,如果输入框有焦点,我们判断输入框的值是否为空,为空就隐藏清除图标,否则就显示它
  • addTextChangedListener(this) 为输入框设置内容改变监听,其实很简单呢,当输入框里面的内容发生改变的时候,我们需要处理显示和隐藏清除小图标,里面的内容长度不为0我们就显示,否是就隐藏,但这个需要输入框有焦点我们才改变显示或者隐藏,为什么要需要焦点,比如我们一个登陆界面,我们保存了用户名和密码,在登陆界面onCreate()的时候,我们把我们保存的密码显示在用户名输入框和密码输入框里面,输入框里面内容发生改变,导致用户名输入框和密码输入框里面的清除小图标都显示了,这显然不是我们想要的效果,所以加了一个是否有焦点的判断这块可以参考http://blog.csdn.net/xiaanming/article/details/11066685/# 这篇文章,还有动画效果等。

猜你喜欢

转载自blog.csdn.net/sinat_27672523/article/details/56839837