Custom Attributes are added to a custom control.

Source: Internet
Author: User

Custom Attributes are added to a custom control.

First, we need to make a request using a customEditTextDisables input of emoticon. Let's talk about the definition and usage of Custom Attributes in custom controls.

The first step is the custom class.EditTextNoEmojiInheritanceEditTextAnd rewrite the three constructor methods at the same time. Note that the three constructor methods must implement the constructor corresponding to the parent class respectively.Super(), Instead of calling another constructor of different parameters of the current classThis(). Otherwise, this control does not have the focus in actual use, and it seems to be causedEditTextInheritanceTextViewThe specific reason is to be checked.

Let's talk about the differences between the three constructor methods. The construction of EditTextNoEmoji (Context context Context) by one parameter is called when the java file is dynamically generated. The construction of EditTextNoEmoji (context Context, attributeSet attrs) is called when the xml file is used for static generation. The construction of the three parameters is basically not used and will not be discussed for the moment. The second parameter attrs in the construction of the two parameters is exactly the property we want to customize. Therefore, setAttrs (context, attrs) is called in this method) set the custom attributes used in the xml file.

 1   public EditTextNoEmoji(Context context) { 2         super(context); 3     } 4  5     public EditTextNoEmoji(Context context, AttributeSet attrs) { 6         super(context, attrs); 7         setAttrs(context, attrs); 8     } 9 10     public EditTextNoEmoji(Context context, AttributeSet attrs, int defStyle) {11         super(context, attrs, defStyle);12         setAttrs(context, attrs);13     }

In step 2, you need to declare the custom attribute to control whether to prohibit the input of emoticon. To facilitate the configuration of this attribute in the xml layout during use, you must first configure this attribute in res/values/styles. custom attribute names in xml. The styleable name of the custom property must be consistent with the Class Name of the custom control. In this style, you can add any custom property, as shown in the following code, added a boolean attribute named canInputEmoji. In addition, the attribute types that can be added include color, boolean, dimension, enum, flag, float, fraction, integer, reference, and string. The attributes defined here are as follows.

1 <declare-styleable name = "EditTextNoEmoji"> 2 <attr name = "canInputEmoji" format = "boolean"/> 3 </declare-styleable>

Next we will go back to the EditTextNoEmoji class relay to continue writing the content in setAttrs (Context context, AttributeSet attrs), and then read the code.

1 // custom attribute to control whether to input emojis 2 private boolean canInputEmoji; 3 4 private void setAttrs (Context context, AttributeSet attrs) {5 TypedArray typedArray = context. obtainStyledAttributes (attrs, R. styleable. editTextNoEmoji); 6 canInputEmoji = typedArray. getBoolean (R. attr. canInputEmoji, false); 7}

As mentioned above, if the control is dynamically used in the java file, it is to call the construction of a parameter, then the above method is not easy to use, so it is necessary to provide encapsulation of this attribute,

1     public boolean canInputEmoji() {2         return canInputEmoji;3     }4 5     public void setCanInputEmoji(boolean canInputEmoji) {6         this.canInputEmoji = canInputEmoji;7     }

At last, rewrite the onTextChanged () method of EditText and determine the Custom Attributes in this method. The Code is as follows.

 1     @Override 2     protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { 3         super.onTextChanged(text, start, lengthBefore, lengthAfter); 4         if(canInputEmoji()){ 5             int index = getSelectionStart() - 1; 6             if (index > 0) { 7                 if (isEmojiChar(text.charAt(index))) { 8                     Editable edit = getText(); 9                     edit.delete(text.length() - 2, text.length());10                 }11             }12         }13     }

The following describes how to determine whether a character is an emoticon character.

1/** 2 * determine whether the character is an emoticon. 3 * @ param inputChar4 * @ return5 */6 private boolean isEmojiChar (char inputChar) {7 return! (InputChar = 0x0) | (inputChar = 0x9) | (inputChar = 0xA) | (inputChar = 0xD) | (inputChar> = 0x20) & inputChar <= 0xD7FF) | (inputChar> = 0xE000) & (inputChar <= 0 xFFFD )) | (inputChar> = 0x10000) & (inputChar <= 0x10FFFF); 8}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.