Android利用已有控件实现自定义控件

Android控件的基本介绍及使用自定义控件的意义

 

        Android 本身提供了很多控件,自定义控件在android中被广泛运用,自定义控件给了我们很大的方便。比如说,一个视图为imageview ,imagebutton ,textview 等诸多控件的组合,用的地方有很多,我们不可能每次都来写3个的组合,既浪费时间,效率又低。在这种情况下,我们就可以自定义一个view来替换他们,不仅提升了效率并且在xml中运用也是相当的美观。
 
属性文件中format可选项 

    自定义控件就需要首先自定义该控件的属性。在开始前,我们需要检查在values目录下是否有attrs.xml,如果没有则创建。下面我们先来了解一下format属性类型都有哪些。

 

format可选项

l"reference" //引用
l"color" //颜色
l "boolean" //布尔值
l "dimension" //尺寸值
l "float" //浮点值
l "integer" //整型值
l "string" //字符串
l "fraction" //百分数 比如200%

 

 

    我们可以在已有的控件的基础上,通过重写相关方法来实现我们的需求。 举一个最简单的例子说明,比如现在的RadioButton按钮只能存在一个text,如果我们想存储key-value对应的键值对,那么我们就需要自定义一个控件。这时定义的控件仅仅比RadioButton多了一个存储key的控件。实现如下:首先在开始前,我们需要检查在values目录下是否有attrs.xml,如果没有则创建。下面把创建的代码贴出来,如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.  <declare-styleable name="RadioButton"><!-- 控件名称-->  
  4.   <attr name="value" format="string"/><!-- 属性名称,类型-->  
  5.  </declare-styleable>  
  6. </resources>  



然后在创建MRadioButton类,该类继承RadioButton。代码如下:

  1. public class MyRadioButton extends android.widget.RadioButton implements OnCheckedChangeListener {  
  2. private String mValue;  
  3. public MyRadioButton(Context context, AttributeSet attrs) {  
  4. super(context, attrs);  
  5.     try {  
  6.     /** 
  7.      * 跟values/attrs.xml里面定义的属性绑定 
  8.      */  
  9.     //TypedArray其实就是一个存放资源的Array  
  10.     TypedArray a = context.obtainStyledAttributes(attrs,  
  11.             R.styleable.RadioButton);  
  12.     this.mValue = a.getString(R.styleable.RadioButton_value);  
  13.            //重复使用对象的styleable属性   
  14.     a.recycle();  
  15.     } catch (Exception e) {  
  16.         e.printStackTrace();  
  17.     }  
  18.     setOnCheckedChangeListener(this);  
  19.     }  
  20. public String getValue() {  
  21. return this.mValue;  
  22. }  
  23. public void setValue(String value) {  
  24. this.mValue = value;  
  25. }  
  26. @Override  
  27. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  28.            System.out.println( "-------Main  te new value is ===>" + this.getValue());     
  29. }  
  30. }  

 

然后再看Layout中的xml代码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:fsms="http://schemas.android.com/apk/res/com.cherry.myview"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.         <com.cherry.view.MyRadioButton  
  8.             android:id="@+id/isPayDepositTrue"  
  9.             android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content"  
  11.             android:text="@string/yes"             
  12.             android:textSize="18sp"  
  13.             fsms:value="true" >  
  14.         </com.cherry.view.MyRadioButton>  
  15. </LinearLayout>  

其中:xmlns:fsms=http://schemas.android.com/apk/res/com.cherry.myview为定义命名空间路径,这里定义完后,就可以对value进行赋值了。

 

 

最后我们来看一下Main函数:

  1. public class MyView extends Activity {  
  2.     private MyRadioButton isPayDepositTrue;  
  3.     /** Called when the activity is first created. */  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         isPayDepositTrue = (MyRadioButton) findViewById(R.id.isPayDepositTrue);  
  9.         //isPayDepositTrue.setValue("false");  
  10.         isPayDepositTrue.setChecked(Boolean.valueOf(isPayDepositTrue.getValue()));  
  11.     }  
  12. }  

编译运行,效果如下:



posted @ 2013-09-25 09:54  Pepper.B  阅读(432)  评论(0编辑  收藏  举报