第一種方法:在XML文件下添加:
android:focusable="true"
android:focusableInTouchMode="true"
第二種方法:直接關(guān)閉輸入法
在onCreate中加上:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
第三中方法:在Edittext中設(shè)置
.setInputType(InputType.TYPE_NULL);
但是會實(shí)現(xiàn)效果梁沧,但是會導(dǎo)致光標(biāo)也無法顯示闪幽,如果想顯示光標(biāo)应闯,你就需要定義一個(gè)方法茸习,在方法內(nèi)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
}
}
}
這時(shí) 你會發(fā)現(xiàn)柔逼,光標(biāo)是出來了 但是輸入文字,光標(biāo)永遠(yuǎn)在前面享钞,怎么辦揍诽,解決思路,監(jiān)聽文字文本內(nèi)容變化監(jiān)聽方法嫩与,在afterTextChanged文字輸入完后,把光標(biāo)移到最后.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 在輸入框內(nèi)增加刪除文字按鈕,就是在Android系統(tǒng)的輸入框右邊加入一個(gè)小圖標(biāo)交排,點(diǎn)擊小圖標(biāo)可以清除輸入框里面的內(nèi)容具體看代碼:
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
public class RMEditText extends EditText implements
OnFocusChangeListener, TextWatcher {
/**
* 刪除按鈕的引用
*/
private Drawable mClearDrawable;
/**
* 控件是否有焦點(diǎn)
*/
private boolean falg;
public ClearEditText(Context context) {
this(context, null);
}
public ClearEditText(Context context, AttributeSet attrs) {
//這里構(gòu)造方法也很重要划滋,不加這個(gè)很多屬性不能再XML里面定義
this(context, attrs, android.R.attr.editTextStyle);
}
public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
//獲取EditText的DrawableRight,假如沒有設(shè)置我們就使用默認(rèn)的圖片
mClearDrawable = getCompoundDrawables()[2];
if (mClearDrawable == null) {
// throw new NullPointerException("You can add drawableRight attribute in XML");
mClearDrawable = getResources().getDrawable(R.drawable.delete_selector);
}
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
//默認(rèn)設(shè)置隱藏圖標(biāo)
setClearIconVisible(false);
//設(shè)置焦點(diǎn)改變的監(jiān)聽
setOnFocusChangeListener(this);
//設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽
addTextChangedListener(this);
}
/**
* 因?yàn)槲覀儾荒苤苯咏oEditText設(shè)置點(diǎn)擊事件,所以我們用記住我們按下的位置來模擬點(diǎn)擊事件
* 當(dāng)我們按下的位置 在 EditText的寬度 - 圖標(biāo)到控件右邊的間距 - 圖標(biāo)的寬度 和
* EditText的寬度 - 圖標(biāo)到控件右邊的間距之間我們就算點(diǎn)擊了圖標(biāo)埃篓,豎直方向就沒有考慮
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())
&& (event.getX() < ((getWidth() - getPaddingRight())));
if (touchable) {
this.setText("");
}
}
}
return super.onTouchEvent(event);
}
/**
* 當(dāng)ClearEditText焦點(diǎn)發(fā)生變化的時(shí)候处坪,判斷里面字符串長度設(shè)置清除圖標(biāo)的顯示與隱藏
*/
@Override
public void onFocusChange(View v, boolean hasFocus) {
this.falg = hasFocus;
if (hasFocus) {
setClearIconVisible(getText().length() > 0);
} else {
setClearIconVisible(false);
}
}
/**
* 設(shè)置清除圖標(biāo)的顯示與隱藏,調(diào)用setCompoundDrawables為EditText繪制上去
* @param visible
*/
protected void setClearIconVisible(boolean visible) {
Drawable right = visible ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
/**
* 當(dāng)輸入框里面內(nèi)容發(fā)生變化的時(shí)候回調(diào)的方法
*/
@Override
public void onTextChanged(CharSequence s, int start, int count,
int after) {
if(falg){
setClearIconVisible(s.length() > 0);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
在xml文件中這樣用
<com.example.edittext.RMEditText
android:hint="輸入文字"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</>
setClearIconVisible()設(shè)置隱藏和顯示清除圖標(biāo)的方法
架专,調(diào)用setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)來設(shè)置上下左右的圖標(biāo)setOnFocusChangeListener(this) 為輸入框設(shè)置焦點(diǎn)改變監(jiān)聽
同窘,如果輸入框有焦點(diǎn),我們判斷輸入框的值是否為空部脚,為空就隱藏清除圖標(biāo)想邦,否則就顯示它addTextChangedListener(this) 為輸入框設(shè)置內(nèi)容改變監(jiān)聽
,其實(shí)很簡單呢委刘,當(dāng)輸入框里面的內(nèi)容發(fā)生改變的時(shí)候丧没,我們需要處理顯示和隱藏清除小圖標(biāo)鹰椒,里面的內(nèi)容長度不為0我們就顯示,否是就隱藏呕童,但這個(gè)需要輸入框有焦點(diǎn)我們才改變顯示或者隱藏漆际,為什么要需要焦點(diǎn),比如我們一個(gè)登陸界面夺饲,我們保存了用戶名和密碼奸汇,在登陸界面onCreate()的時(shí)候,我們把我們保存的密碼顯示在用戶名輸入框和密碼輸入框里面往声,輸入框里面內(nèi)容發(fā)生改變擂找,導(dǎo)致用戶名輸入框和密碼輸入框里面的清除小圖標(biāo)都顯示了,這顯然不是我們想要的效果烁挟,所以加了一個(gè)是否有焦點(diǎn)的判斷這塊可以參考
Android 帶清除功能的輸入框控件ClearEditText,仿IOS的輸入框 這篇文章婴洼,還有動畫效果。