Android instance-Mobile security Defender (ix)-Custom Combo control Properties

Source: Internet
Author: User

First, the goal.

When you use a custom control in a layout file, you set the value directly in the property, similar to the Text property that is set in the TextView control.

Effect

Property settings:

Second, the code implementation.

1, custom namespace, similar to the TextView control inside the Android:text property before the android. In the layout of the layout file where you want to place the custom control (LinearLayout, Relativelayout, and so on), use the Android namespace style to add a custom namespace whose name can be arbitrarily taken (in this case named Custom). The code is: xmlns:custom= "http://schemas.android.com/apk/res/< Package name >", where the package name is full (that is, configuration androidmanifest.xml inside the package= " < package name > ").

Custom namespace Code:

1 xmlns:custom= "Http://schemas.android.com/apk/res/com.example.mobilesafe"
View Code

Therefore, the code in the LinearLayout property is:

1 <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2     xmlns: Custom= "Http://schemas.android.com/apk/res/com.example.mobilesafe"3     android:layout_width= "Match_ Parent "4     android:layout_height=" Match_parent "5     android:orientation=" vertical ">
View Code

2. Define attributes (title, checkbox_on, Checkbox_off) in this example. (can be written in conjunction with the TextView component properties in the Android source code, the address is in the SDK Platforms\android-18\data\res\values.xml file, find < Declare-styleable name= "TextView" > Label, can be written according to the <attr name= "password" format= "boolean"/> Tag inside.

①. Create a new XML file under the Values folder (note that is the Android XML file type), named Arrts.

②. Create <declare-styleable> tag under <resources> tab in Arrts.xml file and build <attr/under <declare-styleable> tab > tags. <attr/> tags are used to customize property names and types, so the number of them can be increased as needed, not three in this case (title, checkbox_on, Checkbox_off).

③. In the <attr/> tag properties, add label properties such as name and format, and the value of name is the custom control property name (title, checkbox_on, Checkbox_off). The value of format is the format of the custom control (all string), and the other label properties refer to the TextView control's writing.

Code for the Attrs.xml file:

1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <resources> 3  4     < Declare-styleable name= "TextView" > 5         <attr name= "title" format= "string"/> 6         < attr name= "Check_off" format= "string"/> 7         <attr name= "check_on" format= "string"/> 8      </declare-styleable> 9 </resources>
View Code

3. Associate a defined property with a custom control class. Custom controls (including system control TextView, and so on) need to instantiate a custom control class (Settingitemview) when placed in a layout file, and a constructor method with two parameters in the custom control class is called Settingitemview (Context Context, AttributeSet attrs), and a construction method with three parameters is called when a style is required, and a constructor with a parameter is called at New. When a layout file invokes a custom control, it encapsulates its properties into a collection of properties of the AttributeSet type (attrs), which is an array of type string, so that the properties can be obtained through the AttributeSet type (ATTRS) related methods.

①. AttributeSet of Attrs type (getattributevalue) in Settingitemview (context context, AttributeSet Attrs) method (String The namespace String name method gets the value of the property, in which the parameter namespace is a namespace, which is the namespace of the property in which you want to get the value (in this case the custom namespace "http./ Schemas.android.com/apk/res/com.example.mobilesafe "), the parameter name is the name of the property that you want to take a value for. The string-type return value of the method is the value of the desired property.

②. In the Settingitemview class, you define a string type of common member variable (title, Check_off, check_on), and then assign the value returned by the Getattributevalue method to these regular member variables.

③. Finally, the return value (title) in ② is filled in with the SetText method of the TextView object (Setting_update_title), and the other two return values (Check_off, Check_ On) is changed in real time according to the status of the checkbox, so it is placed in the setchecked (Boolean checked) method.

The code for the Settingitemview (context context, AttributeSet Attrs) method is as follows:

1  PublicSettingitemview (Context context, AttributeSet attrs) {2         Super(context, attrs);3 Iniview (context);4title = Attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.example.mobilesafe", "title");5Check_off = Attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.example.mobilesafe", "Check_off");6check_on = Attrs.getattributevalue ("Http://schemas.android.com/apk/res/com.example.mobilesafe", "check_on");7 Setting_update_title.settext (title);8}
View Code

④. Changes the text content in real time based on the status of the checkbox. In the Setchecked (Boolean checked) method, pass the IF: Statement to determine the status of the checkbox, and then pass the SetContent (String text) method and pass in the return value to set the display information.

The code for the setchecked (Boolean checked) method is as follows:

 1  public  void  setchecked (boolean   checked) { 2  if   (checked) { 3   SetContent (check_on); 
    4 }{ 5   SetContent (Check_off);  6   7
        setting_update_checkbox.setchecked (checked);  8 } 
View Code

Third, expansion.

You can then define the properties of two custom controls (content, checked) to specify the default display for content and the default check state of the checkbox (True or FALSE). Tip: Use the Getattributebooleanvalue method of AttributeSet (Arrts) to return a Boolean value when associating controls and classes.

Android instance-Mobile security Defender (ix)-Custom Combo control Properties

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.