android EditText 常用属性 焦点处理 各种限制输入(wings6)


// 键盘状态判断

 InputMethodManager imm = (InputMethodManager) PieChartDetailActivity.this.getSystemService(PieChartDetailActivity.this.INPUT_METHOD_SERVICE);
        if (imm.hideSoftInputFromWindow(v.getWindowToken(), 0)) {
            imm.showSoftInput(v, 0);
            Log.d("PieChartDetailActivity", "软键盘已弹出");
            //软键盘已弹出
        } else {
            Log.d("PieChartDetailActivity", "软键盘未弹出");
            //软键盘未弹出
        }

密码框设置

显示内容
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

加密内容
editText.setInputType(0x00000080);

输入框的光标位置移动 需要在子线程中运行

//    子线程调用
     Timer timer = new Timer();
     timer.schedule(new MyTask(), 1);


    //    调用运行更新 方法
    private class MyTask extends TimerTask {
        @Override
        public void run() {
            Message message = new Message();
            message.what = 1;
            mHandler.sendMessage(message);
        }
    }

    //    子线程
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            //    更新UI操作
//            移动光标位置
            CharSequence text = editTextUserpassword.getText();
            if (text instanceof Spannable) {
                Spannable spanText = (Spannable) text;
                Selection.setSelection(spanText, text.length());
            }
        }
    };


editText 焦点 兼左边图标处理设置 就输入密码 左边的小图标也要跟着变换

        nav_up = getResources().getDrawable(R.drawable.password1_green);
        nav_up1 = getResources().getDrawable(R.drawable.password1);
        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
        nav_up1.setBounds(0, 0, nav_up1.getMinimumWidth(), nav_up1.getMinimumHeight());
        
        editText1.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
// 此处为得到焦点时的处理内容
                    editText1.setBackground(getResources().getDrawable(R.drawable.border_bootm));
                    editText1.setCompoundDrawables(nav_up, null,null , null);
                } else {
// 此处为失去焦点时的处理内容
                    editText1.setBackground(getResources().getDrawable(R.drawable.border_bootm_gray));
                    editText1.setCompoundDrawables(nav_up1, null,null , null);
                }
            }
        });


EditText 文字外观设置

android:textAppearance  设置文字外观。

如“?android:attr/textAppearanceLargeInverse
”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。
可设置的值如下:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/
textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/
textAppearanceSmall/textAppearanceSmallInverse

editText.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下划线

editText.getPaint().setAntiAlias(true);//抗锯齿

editText.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //中划线

editText.setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);  // 设置中划线并加清晰 

editText.getPaint().setFlags(0);  // 取消设置的的划线

排除特殊字符 和空格之类的东西 只能输入 英文和 中文 EditText

    public void setIfSpecialCharacters(final EditText ed) {
        //ed.setOnClickListener(this);  // 有些要用的
        ed.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
                String va = ed.getText().toString();
                if (va.length() > 0) {
                    String reg = "[^a-zA-Z\u4E00-\u9FA5]";

                    if (va.replaceAll(reg, "").length() < va.length()) {
                        ed.setText(Util.ReString(va));
                    }

                }
                System.out.println(Util.ReString(va));
                ed.setSelection(ed.getText().length());
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

            }
        });
    }


判断是否有中文字符

  /**
     * 验证中文
     *
     * @param chinese 中文字符
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean checkChinese(String chinese) {
        String regex = "^[\u4E00-\u9FA5]+$";
        return Pattern.matches(regex, chinese);
    }


EditText 在addTextChangedListener 里面操作 的焦点重复调用问题处理

通过移除焦点和 添加焦点 解决递归操作问题 打比方说要清空掉用户输入的东西 就会有递归的问题了

/**设置空数据  去除焦点!!!!!*/
et_content.removeTextChangedListener(this);  


/**收起键盘*/
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
holder.et_content.setText("");


/**设置空数据后 把焦点加回来 核心代码 */
holder.et_content.addTextChangedListener(this);



Edit重写

et_searchData.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

只能输入 数字和英文 限制 直接在xml里面写就好了

android:inputType="number"
android:digits="1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"

EditText 输入类型

android:inputType="numberDecimal" 输入金额

android:singleLine="true" //禁止输入回车

自动换行

android:inputType="textMultiLine"  

EditText 无法编辑

android:focusable="false"

缩回输入法

InputMethodManager m = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
m.hideSoftInputFromWindow(Et_search.getWindowToken(), 0);//比如EditView

禁止EditText输入特殊字符

/**
 * 禁止EditText输入特殊字符
 * @param editText
 */
public static void setEditTextInhibitInputSpeChat(EditText editText){

    InputFilter filter=new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            String speChat="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
            Pattern pattern = Pattern.compile(speChat);
            Matcher matcher = pattern.matcher(source.toString());
            if(matcher.find())return "";
            else return null;
        }
    };
    editText.setFilters(new InputFilter[]{filter});
}

//监听输入框禁止输入空格

Et_search.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
                              int count) {

        if (s.toString().contains(" ")) {
            String[] str = s.toString().split(" ");
            String str1 = "";
            for (int i = 0; i < str.length; i++) {
                str1 += str[i];
            }
            Et_search.setText(str1);
            Et_search.setSelection(start);

        }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});


只能输入中文 的EditText

    public void setEd(EditText et_chinese){
        //输入过滤器可以实现很多效果
        InputFilter filter = new InputFilter() {public CharSequence filter(CharSequence source, int start, int end,
                                                                           Spanned dest, int dstart, int dend) {
                for (int i = start; i < end; i++) {
                    if (!isChinese(source.charAt(i))) {
                        return "";
                    }
                }
                return null;
            }
        };
        et_chinese.setFilters(new InputFilter[]{filter});
        //如果想要再实现输入字符数量的限制,可以这么写,如果限制字符数为6,就在LengthFilter中传入参数6
        //et_chinese.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(6)});

    }

    /**
     * 判定输入汉字,通过Unicode表
     *
     * @param c
     * @return
     */
    public boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }


在最新手机号码正则

^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$

默认不打开输入法 清单文件设置

 android:windowSoftInputMode="adjustUnspecified|stateAlwaysHidden"

代码里面执行
在 onCreate切记要倒计时 直接调用不行 应该有更好的写法吧

//        弹出输入框
        new Handler().postDelayed(new Runnable(){
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 300);

你可能感兴趣的:(android EditText 常用属性 焦点处理 各种限制输入(wings6))