Android 自定义控件
1 package net.etmobile.widget;
2
3 import net.etmobile.R;
4
5 public class SubText extends LinearLayout {
6 private final static String TAG = "SubText";
7
8 private TextView titleTextView;
9 private TextView subTextView;
10 private View mView;
11 private OnClickListener mClickListener;
12
13
14 public SubText(Context context) {
15 super(context);
16 }
17
18 public SubText(Context context, AttributeSet attrs) {
19 super(context, attrs);
20
21 LayoutInflater layoutInflater = LayoutInflater.from(context);
22 mView = layoutInflater.inflate(R.layout.widget_subtext, this);
23
24 LinearLayout linear = (LinearLayout)mView.findViewById(R.id.widget_subtext);
25 linear.setOnClickListener(new OnClickListener() {
26
27 public void onClick(View v) {
28 Log.d(TAG,"LinearLayout click");
29 if (mClickListener != null)
30 mClickListener.onClick(SubText.this);
31 }
32 });
33
34 titleTextView = (TextView) findViewById(R.id.sub_title);
35 subTextView = (TextView) findViewById(R.id.sub_text);
36
37 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SubText);
38 setTitleText(a.getString(R.styleable.SubText_titleText));
39 setSubText(a.getString(R.styleable.SubText_subText));
40 a.recycle();
41 }
42
43 public CharSequence getTitleText() {
44 return titleTextView.getText();
45 }
46
47 public void setTitleText(CharSequence titleTextView) {
48 this.titleTextView.setText(titleTextView);
49 }
50
51 public CharSequence getSubText() {
52 return subTextView.getText();
53 }
54
55 public void setSubText(CharSequence subTextView) {
56 this.subTextView.setText(subTextView);
57 }
58
59 @Override
60 public void setOnClickListener(OnClickListener l) {
61 if (!isClickable()) {
62 setClickable(true);
63 }
64 mClickListener = l;
65 }
66
67 @Override
68 public String toString() {
69 return getTitleText() + "/" + getSubText();
70 }
71 }
XML layout 布局文件
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/widget_subtext"
4 style="@style/form_item"
5 android:layout_width="fill_parent"
6 android:layout_height="match_parent"
7 android:descendantFocusability="blocksDescendants"
8 android:focusable="true"
9 android:clickable="true"
10 android:orientation="vertical" >
11
12 <TextView
13 android:id="@+id/sub_title"
14 style="@style/textLarge"
15 android:layout_width="fill_parent"
16 android:layout_height="0dp"
17 android:layout_weight="0.5"
18 android:ellipsize="end"
19 android:clickable="false"
20 android:focusable="false"
21 android:gravity="bottom|left"
22 android:singleLine="true" />
23
24 <TextView
25 android:id="@+id/sub_text"
26 style="@style/textMedium"
27 android:layout_width="fill_parent"
28 android:layout_height="0dp"
29 android:layout_weight="0.5"
30 android:clickable="false"
31 android:focusable="false"
32 android:gravity="top|left" />
33
34 </LinearLayout>
在values下定义一个XML文件,一般为 attrs.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <declare-styleable name="SubText">
4 <attr name="titleText" format="reference|string"></attr>
5 <attr name="subText" format="reference|string"></attr>
6 <attr name="Oriental">
7 <enum name="Horizontal" value="1"></enum>
8 <enum name="Vertical" value="0"></enum>
9 </attr>
10 </declare-styleable>
11 </resources>
使用方式
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:ns="http://schemas.android.com/apk/res/net.etmobile"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical" >
7
8 <net.etmobile.widget.SubText
9 android:id="@+id/pue_wom_inf_id_no"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 ns:subText="xxxxx"
13 ns:titleText="xxxx" />
14
15 <net.etmobile.widget.SubText
16 android:id="@+id/pue_wom_inf_no_id"
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content"
19 ns:subText="xxxxx"
20 ns:titleText="xxxx" />
21
22 <net.etmobile.widget.SubText
23 android:id="@+id/pue_wom_inf_no1_id"
24 android:layout_width="wrap_content"
25 android:layout_height="wrap_content"
26 ns:subText="xxxxx"
27 ns:titleText="xxxx" />
28
29 </LinearLayout>
注意 xmlns:ns="http://schemas.android.com/apk/res/net.etmobile" 其中 net.etmobile 是你的工程包名,不是widget所在的包名
解决外部不能监听用户单击的问题的方式比较恶心,我一直觉得有其它的方式可以解决这种问题,求高人指点。