EditTextView related control

EditTextView related controls

Recently, I have done some simple work, and more about the use of edittextview, to summarize

1. Get focus, cancel focus

How to automatically obtain focus, how to cancel the automatic focus.

I wrote something before, refer to the link: https://www.cnblogs.com/wisdomzhang/p/12303577.html

2. Get focus monitoring

et_pwd.setOnFocusChangeListener(new View.OnFocusChangeListener() {            @Override            
    public void onFocusChange(View v, boolean hasFocus) {                		if (hasFocus) {                 
    		
    	} else {     
        	//to do something
    	}          
   }      
});

3. Text input monitoring

//文本改变 里面的有什么
et_pwd.addTextChangedListener(new TextWatcher() {    
        @Override    
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {  
        //文本改变之前 s从start开始,会有count个字符被替换,最后成为after
        }
    
        @Override    
        public void onTextChanged(CharSequence s, int start, int before, int count) {     
         //文本改变中 s从start开始,有before个字符已经被替换为count个字符
            if (mpwd == null || s.length() != mpwd.length()) {            
                mpwd = s.toString();      
            }   
        }   
    
        @Override    
        public void afterTextChanged(Editable s) {   
        //文本改变后
        }
    });

4. Text input filtering InputFilter

InputFilter[] filters = { new InputFilter() { 
    @Override   
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        //to do something
    }
},	...};
et_value.setFilters(filters); //可以添加多种过滤

There is one input. The uncommon problem is input association. The input method automatically adds some text to you, at this time, it will add a filter to him.

5. Text type control

a. There are two options for the type of text input:

  • The code control is as follows:

    et_value.setInputType(InputType.TYPE_CLASS_TEXT | ...);
    
  • xml control:

    android:inputType="textWebPassword"
    

b. Whether the password is visible

//显示密码
et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//隐藏密码
et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());

c. Type control input

  • Only enter numbers, only enter English characters, enter numbers and letters, only enter Chinese characters and so on.

    There are some solutions:

    • Use the attributes of xml, digits = "123456789" digits = "12345xml" and so on to control the characters entered.

    • Use textwatcher to control the input and filter out the input content

    • //控制只能输入指定的特殊字符
      editText.setKeyListener(new NumberKeyListener(){
      	@Override  
          protected char[] getAcceptedChars() {  
              return new char[] { '1', '2', '3', '4', '5', '6', '7', '8','9', '0' };  
          }  
          
      	@Override  
          public int getInputType() {  
              // TODO Auto-generated method stub   
              return android.text.InputType.TYPE_CLASS_PHONE;  
          }  
      });
      
    • Chinese only

      @Override
      public void afterTextChanged(Editable s) {
      	//中文过滤
      	String inputStr = clearLimitStr(regex, str);
      	et_value.removeTextChangedListener(this);
      	// et.setText方法可能会引起键盘变化,所以用editable.replace来显示内容
      	s.replace(0, s.length(), inputStr.trim());
      	et_value.addTextChangedListener(this);
      }
      
  • The length of the control input, lengthit

    If the length exceeds the width of the control, you can slide to display the text, which can be set as follows:

    et_value.setSingleLine(true);
    et_value.setHorizontallyScrolling(true);
    
    //中文的长度限制
    new InputFilter() { //中文长度限制,50
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            int keep = itemValue.getLength() - (dest.length() - (dend - dstart));
            if (keep <= 0) {
                Toast.makeText(m, "最多只能输入" + mMax + "个汉字", Toast.LENGTH_SHORT).show();
                return "";
            } else if (keep >= end - start) {
                return null;
            } else {
                keep += start;
              	if (Character.isHighSurrogate(source.charAt(keep - 1))) {
                    --keep;
                    if (keep == start) {
                        return "";
                    }
                }
                return source.subSequence(start, keep);
            }
        }
    }
    

6. Pop-up keyboard

//弹出键盘
InputMethodManager imm = (InputMethodManager) LoginActivity.this .getSystemService(Context.INPUT_METHOD_SERVICE);                  
imm.showSoftInput(et_pwd, InputMethodManager.RESULT_SHOWN);               
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); 

7. Modification of style

  • Focus cursor color

    //属性
    android:textCursorDrawable="@drawable/shape_login_color_cursor"
    //样式
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <size android:width="1dp" />
        <solid android:color="@color/color_login_bg_normal" />
    </shape>
    
  • Underline color

    • Do not underline background = "@null" or set to background = "#ffffff"

    • android:theme Modification

      <style name="EditTextStyle">
          <!--表示控件默认的颜色-->
          <item name="android:colorControlNormal">#ccc</item>
          <!--表示控件被激活时的颜色-->
          <item name="android:colorControlActivated">#ccc</item>
      </style>
      

Guess you like

Origin www.cnblogs.com/wisdomzhang/p/12683500.html