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