实现效果:
第一个效果是在输入框中添加了一个时间选择的按钮,当点击按钮时弹出对应的对话框
第二个效果是在输入框中添加了一个textview,然后textview可以在XML文件中进行配置,主要介绍第二个,其实逻辑都是一样的
1. 在values下面添加文件attrs.xml文件,然后添加自己自定义属性,如下
<resources>
<declare-styleable name="unit">
<attr name="unitStr" format="string" />
<attr name="textColor" format="color" />
</declare-styleable>
</resources>
</pre><pre name="code" class="html">2. 因为我是使用的两个控件组合在一起,所以需要定义一个组合控件的样式,在layout下面添加xml,edit_with_unit.xml
<pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/dataText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edittext"
android:minEms="4"
android:paddingRight="25dip"
android:textSize="@dimen/text_size_22"
android:textStyle="bold" />
<TextView
android:id="@+id/dataUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/dataText"
android:gravity="bottom"
android:paddingTop="5dip"
android:textSize="@dimen/text_size_22"
/>
</RelativeLayout>
</pre><pre name="code" class="html">3. 定义自定义控件类,(此处以第二种效果为例,实在是文本框中带控钮,可以直接在此类中做button的监听)
</pre><pre name="code" class="html">
public class UnitView extends LinearLayout {
private String unitStr;
private EditText dataText;
private TextView unitText;
private int textColor;
public UnitView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public UnitView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.unit);
unitStr = typedArray
.getString(R.styleable.unit_unitStr);
textColor = typedArray.getColor(R.styleable.unit_textColor, Color.BLACK);
/*textHintLabel = typedArray
.getString(R.styleable.SelfImageTextAttr_text_hint_label);
image_id = typedArray.getResourceId(
R.styleable.SelfImageTextAttr_image_id, 0);
edttext_inputtype = typedArray
.getString(R.styleable.SelfImageTextAttr_edttext_inputtype);
label_width = Math.round(typedArray.getDimension(
R.styleable.SelfImageTextAttr_label_width, 0));
defaultText = typedArray
.getString(R.styleable.SelfImageTextAttr_default_text);
String txtReadOnly = typedArray
.getString(R.styleable.SelfImageTextAttr_text_readonly);
if (txtReadOnly != null && txtReadOnly.equals("true")) {// comment by
// danielinbiti
isReadOnly = false;
}*/
this.init(context);
typedArray.recycle();
}
public void init(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.edit_with_unit, this, true);
unitText = (TextView)view.findViewById(R.id.dataUnit);
dataText = (EditText)view.findViewById(R.id.dataText);
dataText.setTextColor(textColor);
unitText.setText(unitStr);
}
public String getUnitStr() {
return unitStr;
}
public void setUnitStr(String unitStr) {
this.unitStr = unitStr;
}
public EditText getDataText() {
return dataText;
}
public void setDataText(EditText dataText) {
this.dataText = dataText;
}
public TextView getUnitText() {
return unitText;
}
public void setUnitText(TextView unitText) {
this.unitText = unitText;
}
public int getTextColor() {
return textColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
}
</pre><pre name="code" class="java">4. 自定义控件的使用
(1).首先必须在需要使用配置的顶部声明一下自定义属性的前缀,<pre name="code" class="html">xmlns:前缀名="http://schemas.android.com/apk/res/自定义控件类包名",<span style="color:#ff6666;">一定要注意此处是包名,我开始写到了类名,但怎么都找不到我的属性,研究了半天,后来才发现应该是包名。。。伤死心了,浪费了我小半天</span>
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:unit="http://schemas.android.com/apk/res/com.lxdz.capacity.activity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background_main"
android:orientation="vertical" >
<pre name="code" class="html"><com.lxdz.capacity.activity.UnitView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10.0dip"
android:layout_weight="1"
android:imeOptions="actionNext"
android:textSize="@dimen/text_size_20"
unit:unitStr="kV" />
<pre name="code" class="html"></LinearLayout>
好了,到些就结束了,我还没怎么写过技术博客,写的不太好,希望有不明白或者大虾多多指导
</pre><br /><br /><pre>