Problem adding custom properties to custom controls

First, let's mention the requirements. A user-defined EditText is used to prohibit the input of expressions. Talk about the definition and use of custom attributes in custom controls.

The first step, of course, is to inherit EditText from the custom class edittextnoemoji and override the three constructor methods at the same time. It should be noted that among the three construction methods, the corresponding construction methods of the parent class must be implemented respectively, that is, the three super(); Another form of constructor that calls different parameters of the current class cannot appear, that is, this() cannot appear. Otherwise, the control will not get the focus in actual use. It seems that EditText inherits textview. The specific reason remains to be found.

Talk about the differences between these three construction methods. The construction of edittextnoemoji (context context) with one parameter is called when it is dynamically generated in the used java file, and the construction of edittextnoemoji (context context, attributeset attrs) with two parameters is called when it is statically generated in the used XML file. The construction of three parameters will not be used for the time being. The second parameter attrs in the two parameter structure is exactly the attribute we want to customize, so we call setAttrs (context, attrs) in the method to set the custom attribute used in the XML file.

In the second step, you need to declare a custom attribute to control whether to prohibit the input of expressions. At the same time, in order to facilitate the configuration of the attribute in the XML layout when the control is used, you should first customize the attribute name in RES / values / styles.xml. The styleable name of the custom attribute must be consistent with the class name of the custom control. Any custom attribute can be added under this style. The following code adds a boolean type attribute named caninputemoji. In addition, ten attribute types can be added, including color, Boolean, dimension, enum, flag, float, fraction, integer, reference, string, etc. The attributes defined here are as follows.

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

Next, I'll go back to the edittextnoemoji class just now and continue to write the contents of setattrs (context, attributeset attrs), and then look at the code.

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

Finally, you can override the ontextchanged () method of EditText, and the custom attribute is judged in this method. The code is as follows.

The method of judging whether the character is an emoticon character is pasted below.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>