The basics of the Android custom control family

Source: Internet
Author: User

I. Overview

In Android development, many UI controls often need to be customized to meet the needs of the application or to achieve more results, followed by a series to introduce the custom control, here is more through a number of cases to learn, this series has some typical applications, Well, you can also go to the innovation to develop some better UI, this time with a simple case to master some basic knowledge-how to define properties in the custom control.

Second, the realization of a simple radiobutton

 1. Write type Mradiobutton extension radiobutton

public class Mradiobutton extends RadioButton {...    }

 2. In the Mradiobutton class, custom attributes

We can define our own properties in the control, we can define multiple properties, but we must encapsulate the Set/get method, which is written by specification. such as the Mvalue property, like the following code

Private String Mvalue;    Public String Getmvalue () {        return mvalue;    }    public void Setmvalue (String mvalue) {        this.mvalue = mvalue;    }

  3. Write Attrs.xml resources for custom attributes

The resource file is placed in the Res/values directory with the following contents:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable    name= "Mradiobutton" >            <!– Property Name--        <attr name= "value" format= "string"/>    </declare-styleable></ Resources>

 4. Define constructors in the Mradiobutton class, initialize properties

            Public Mradiobutton (Context context) {        super (context);    } Public Mradiobutton (context context, AttributeSet attrs, int defstyle) {        Super (context, attrs, Defstyle);    }        Public Mradiobutton (context context, AttributeSet Attrs) {        Super (context, attrs);        Load a name from the Attrs.xml. Mradiobutton ' declare-styleable resources    TypedArray Tarray = context.obtainstyledattributes (Attrs, R.styleable.mradiobutton);        Associates the property value with the attribute in the class mvalue    this.mvalue = tarray.getstring (r.styleable.mradiobutton_value);        Recycle Tarray Object    tarray.recycle ();    }

5. Add the Mradiobutton component to the layout file in Mainactivity, as shown below

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    xmlns:jereh=" Http://schemas.android.com/apk/res/com.jereh. View "    android: Layout_width= "Match_parent"    android:layout_height= "match_parent"    tools:context= " Com.example.zdyview.MainActivity ">    <com.itc.zidingyiview.mradiobutton        android:layout_width=" Match_parent "        android:layout_height=" match_parent "        android:id=" @+id/mrb "        jereh:value=" Hello "        /></relativelayout>

6, Mainactivity Code:

public class Mainactivity extends Activity {    private Mradiobutton RB;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        rb= (Mradiobutton) Super.findviewbyid (R.ID.MRB);        Rb.setonclicklistener (New View.onclicklistener () {            @Override public            void OnClick (View v) {Toast.maketext ( Mainactivity.this, Rb.getmvalue (), Toast.length_long). Show ();}}        );}    }

The basics of the Android custom control family

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.