Android自定义控件

出自:http://www.92coding.com/blog/index.php/archives/143.html


今天和大家分享下组合控件的使用。很多时候android自定义控件并不能满足需求,如何做呢?很多方法,可以自己绘制一个,可以通过继承基础控件来重写某些环节,当然也可以将控件组合成一个新控件,这也是最方便的一个方法。今天就来介绍下如何使用组合控件,将通过两个实例来介绍。
第一个实现一个带图片和文字的按钮,如图所示:
<a href="https://i-blog.csdnimg.cn/blog_migrate/d9e24c1899bfe0c7fcfe1e11dbc616e4.png" class="cboxElement" rel="example4" 143"="" style="text-decoration: initial; color: rgb(1, 150, 227);">若水工作室
整个过程可以分四步走。第一步,定义一个layout,实现按钮内部的布局。代码如下:
custom_button.xml

1 <?xml version="1.0" encoding="utf-8"?> 
2 <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android" 
3     android:orientation="horizontal" 
4     android:layout_width="fill_parent" 
5     android:layout_height="fill_parent"
6     
7  <ImageView 
8   android:id="@+id/iv" 
9      android:layout_width="wrap_content" 
10      android:layout_height="wrap_content" 
11      android:layout_gravity="center_vertical"
12      android:paddingLeft="10.0dip"
13      android:paddingTop="10.0dip"
14      android:paddingBottom="10.0dip"
15      /> 
16  <TextView 
17   android:id="@+id/tv" 
18      android:layout_width="wrap_content" 
19      android:layout_height="wrap_content" 
20      android:textColor="#ffffff" 
21      android:layout_marginLeft="8dip" 
22      android:layout_gravity="center_vertical" 
23      android:paddingLeft="5.0dip"
24      android:paddingTop="10.0dip"
25      android:paddingBottom="10.0dip"
26      android:paddingRight="10.0dip"
27      android:textSize="18.0sp"
28      /> 
29 </LinearLayout>

这个xml实现一个左图右字的布局,接下来写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。代码如下:
CustomButton.java

1 package com.szy.customview;
2  
3 import android.content.Context;
4 import android.util.AttributeSet;
5 import android.view.LayoutInflater;
6 import android.widget.ImageView;
7 import android.widget.LinearLayout;
8 import android.widget.TextView;
9  
10 /**
11  *@author coolszy
12  *@date 2011-12-20
14  */
15 public class CustomButton extends LinearLayout
16 {
17  
18  private ImageView iv;
19  private TextView tv;
20  
21  public CustomButton(Context context)
22  {
23   this(context, null);
24  }
25  
26  public CustomButton(Context context, AttributeSet attrs)
27  {
28   super(context, attrs);
29   // 导入布局
30   LayoutInflater.from(context).inflate(R.layout.custom_button, this, true);
31   iv = (ImageView) findViewById(R.id.iv);
32   tv = (TextView) findViewById(R.id.tv);
33  }
34  
35  /**
36   * 设置图片资源
37   */
38  public void setImageResource(int resId)
39  {
40   iv.setImageResource(resId);
41  }
42  
43  /**
44   * 设置显示的文字
45   */
46  public void setTextViewText(String text)
47  {
48   tv.setText(text);
49  }
50 }

第三步,在需要使用这个自定义控件的layout中加入这控件,只需要在xml中加入即可。方法如下:
main.xml

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2  android:layout_width="fill_parent" 
3  android:layout_height="fill_parent" 
4  android:orientation="vertical" 
5  
6  <com.szy.customview.CustomButton
7      android:id="@+id/bt_confirm" 
8      android:layout_width="wrap_content"
9      android:layout_height="wrap_content" 
10      android:background="@drawable/button_bg" 
11      /> 
12  <com.szy.customview.CustomButton
13      android:id="@+id/bt_cancel" 
14      android:layout_width="wrap_content"
15      android:layout_height="wrap_content" 
16      android:background="@drawable/button_bg" 
17     /> 
18 </LinearLayout>

注意的是,控件标签使用完整的类名即可。为了给按钮一个点击效果,你需要给他一个selector背景,这里就不说了。
最后一步,即在activity中设置该控件的内容。当然,在xml中也可以设置,但是只能设置一个,当我们需要两次使用这样的控件,并且显示内容不同时就不行了。在activity中设置也非常简单,我们在CustomButton这个类中已经写好了相应的方法,简单调用即可。代码如下:

1 package com.szy.customview;
2  
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.view.View;
6 import android.view.View.OnClickListener;
7  
8 public class MainActivity extends Activity
9 {
10  private CustomButton btnConfirm;
11  private CustomButton btnCancel;
12  
13  @Override
14  public void onCreate(Bundle savedInstanceState)
15  {
16   super.onCreate(savedInstanceState);
17   setContentView(R.layout.main);
18   btnConfirm = (CustomButton) findViewById(R.id.bt_confirm);
19   btnCancel = (CustomButton) findViewById(R.id.bt_cancel);
20  
21   btnConfirm.setTextViewText("确定");
22   btnConfirm.setImageResource(R.drawable.confirm);
23   btnCancel.setTextViewText("取消");
24   btnCancel.setImageResource(R.drawable.cancel);
25  
26   btnConfirm.setOnClickListener(new OnClickListener()
27   {
28    @Override
29    public void onClick(View v)
30    {
31     // 在这里可以实现点击事件
32    }
33   });
34  }
35 }

这样,一个带文字和图片的组合按钮控件就完成了。这样梳理一下,使用还是非常简单的。组合控件能做的事还非常多,主要是在类似上例中的CustomButton类中写好要使用的方法即可。
再来看一个组合控件,带删除按钮的EidtText。即在用户输入后,会出现删除按钮,点击即可取消用户输入。
定义方法和上例一样。首先写一个自定义控件的布局:
custom_editview.xml

1 <?xml version="1.0" encoding="utf-8"?> 
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
3     android:layout_width="fill_parent" 
4     android:layout_height="fill_parent" 
5     
6  <EditText 
7      android:id="@+id/et" 
8      android:layout_width="fill_parent" 
9      android:layout_height="wrap_content" 
10      android:singleLine="true" 
11      /> 
12  <ImageButton 
13      android:id="@+id/ib" 
14      android:visibility="gone" 
15      android:src="@drawable/cancel" 
16      android:layout_width="wrap_content" 
17      android:layout_height="wrap_content" 
18      android:background="#00000000" 
19      android:layout_alignRight="@+id/et" /> 
20 </RelativeLayout>

实现输入框右侧带按钮效果,注意将按钮隐藏。然后写一个CustomEditView类,实现删除用户输入功能。这里用到了TextWatch这个接口,监听输入框中的文字变化。使用也很简单,实现他的三个方法即可。看代码:
CustomEditView.java

1 package com.szy.customview;
2  
3 import android.content.Context;
4 import android.text.Editable;
5 import android.text.TextWatcher;
6 import android.util.AttributeSet;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.widget.EditText;
10 import android.widget.ImageButton;
11 import android.widget.LinearLayout;
12  
13 /**
14  *@author coolszy
15  *@date 2011-12-20
17  */
18 public class CustomEditView extends LinearLayout implements EdtInterface
19 {
20  ImageButton ib;
21  EditText et;
22  
23  public CustomEditView(Context context)
24  {
25   super(context);
26  
27  }
28  public CustomEditView(Context context, AttributeSet attrs)
29  {
30   super(context, attrs);
31   LayoutInflater.from(context).inflate(R.layout.custom_editview, this, true);
32   init();
33  
34  }
35  private void init()
36  {
37   ib = (ImageButton) findViewById(R.id.ib);
38   et = (EditText) findViewById(R.id.et);
39   et.addTextChangedListener(tw);// 为输入框绑定一个监听文字变化的监听器
40   // 添加按钮点击事件
41   ib.setOnClickListener(new OnClickListener()
42   {
43    @Override
44    public void onClick(View v)
45    {
46     hideBtn();// 隐藏按钮
47     et.setText("");// 设置输入框内容为空
48    }
49   });
50  
51  }
52  // 当输入框状态改变时,会调用相应的方法
53  TextWatcher tw = new TextWatcher()
54  {
55    
56   @Override
57   public void onTextChanged(CharSequence s, int start, int before, int count)
58   {
59    // TODO Auto-generated method stub
60  
61   }
62    
63   @Override
64   public void beforeTextChanged(CharSequence s, int start, int count, int after)
65   {
66    // TODO Auto-generated method stub
67  
68   }
69    
70   // 在文字改变后调用
71   @Override
72   public void afterTextChanged(Editable s)
73   {
74    if (s.length() == 0)
75    {
76     hideBtn();// 隐藏按钮
77    } else
78    {
79     showBtn();// 显示按钮
80    }
81   }
82  };
83  
84  @Override
85  public void hideBtn()
86  {
87   // 设置按钮不可见
88   if (ib.isShown())
89    ib.setVisibility(View.GONE);
90  
91  }
92  
93  @Override
94  public void showBtn()
95  {
96   // 设置按钮可见
97   if (!ib.isShown())
98   {
99    ib.setVisibility(View.VISIBLE);
100   }
101  }
102 }
103  
104 interface EdtInterface
105 {
106  public void hideBtn();
107  public void showBtn();
108 }

在TextWatch接口的afterTextChanged方法中对文字进行判断,若长度为0,就隐藏按钮,否则,显示按钮。
另外,实现ImageButton(即那个叉)的点击事件,删除输入框中的内容,并隐藏按钮。
后面两步的实现就是加入到实际布局中:
main.xml

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2  android:layout_width="fill_parent" 
3  android:layout_height="fill_parent" 
4  android:orientation="vertical" 
5  >
6  <com.szy.customview.CustomEditView
7   android:layout_width="fill_parent"
8      android:layout_height="wrap_content" 
9     />
10 </LinearLayout
11   

最后显示效果如图:
<a href="https://i-blog.csdnimg.cn/blog_migrate/1efd3c208250859beb5aac8cb7379e7d.png" class="cboxElement" rel="example4" 143"="" style="text-decoration: initial; color: rgb(1, 150, 227);">若水工作室

源码下载地址:http://115.com/file/aq5qvh67


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值