EditText去掉光標下劃線以及點擊隱藏hint

EditText使用很廣泛,今天使用中需要去掉光標以及下劃線,還需要點擊後隱藏hint

去掉下劃線 android:background="@null"
去掉光標 android:cursorVisible="false"

點擊後隱藏hint 需要寫一個焦點變化事件監聽

public  View.OnFocusChangeListener onFocusAutoClearHintListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            EditText editText=(EditText)v;
            if (!hasFocus) {// 失去焦點
                editText.setHint(editText.getTag().toString());
            } else {
                String hint=editText.getHint().toString();
                editText.setTag(hint);
                editText.setHint("");
            }
        }
    };

然後再edittext綁定就行了

 meditSettingNickName.setOnFocusChangeListener(onFocusAutoClearHintListener);

但是在一進入界面edit就會有焦點所以需要把焦點聚集在別的控件上,需要在佈局中給EditText的容器控件添加以下兩個屬性,把焦點聚集在容器控件上。

android:focusable="true"
android:focusableInTouchMode="true"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章