最近的项目中界面上频繁用到类似于登录注册的输入框,包括标签、输入框,于是想自定义一个包括标签和输入框的控件,一举多用。
一、定义控件布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="43dp"
android:orientation="vertical" >
<LinearLayout
style="@style/style_edit_layout"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview_label"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="@color/lable_light_black"
android:textSize="16sp"
android:textStyle="bold" />
<EditText
android:id="@+id/et_input"
style="@style/dx_edit_text"
android:layout_marginTop="5dp"
android:textColorHint="@color/edit_hint_light_gray"
/>
</LinearLayout>
<View
style="@style/dx_horizontal_line"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="5dp" />
</LinearLayout>
二、在attr文件中定义属性
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="EditLayout">
<attr name="editLable" format="reference|string"/>
<attr name="editHint" format="reference|string"/>
<attr name="inputType" format="integer"/>
<attr name="maxLength" format="integer"/>
</declare-styleable>
</resources>
三、定义继承自LinearLayout的EditInfoLayout类
public class EditInfoLayout extends LinearLayout {
LayoutInflater inflater ;
TextView textLable;
EditText editInput;
public EditInfoLayout(Context context) {
super(context);
}
public EditInfoLayout(Context context, AttributeSet attrs) {
super(context, attrs);
inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.layout_edittext, null);
textLable = (TextView) view.findViewById(R.id.textview_label);
editInput = (EditText) view.findViewById(R.id.et_phone_input);
int resourceId = -1;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EditLayout);
int typedArrayCount = typedArray.getIndexCount();
for(int i=0; i<typedArrayCount; i++){
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.EditLayout_editLable://标签名称
resourceId = typedArray.getResourceId(R.styleable.EditLayout_editLable, 0);
textLable.setText((resourceId > 0 ? typedArray.getResources().getText(resourceId)
: typedArray.getResources().getString(R.styleable.EditLayout_editLable)));
break;
case R.styleable.EditLayout_editHint://设置editText的hint值
resourceId = typedArray.getResourceId(R.styleable.EditLayout_editHint, 0);
editInput.setHint((resourceId > 0 ? typedArray.getResources().getText(resourceId)
: typedArray.getResources().getString(R.styleable.EditLayout_editHint)));
break;
case R.styleable.EditLayout_inputType://设置editText的inputType
resourceId = typedArray.getInt(R.styleable.EditLayout_inputType, 0);
if(resourceId == 2){
editInput.setInputType(InputType.TYPE_CLASS_NUMBER);
}else if(resourceId == 3){
editInput.setInputType(InputType.TYPE_CLASS_PHONE);
}else if(resourceId == 12){
editInput.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD);
}else{
editInput.setInputType(InputType.TYPE_CLASS_TEXT);
}
case R.styleable.EditLayout_maxLength://设置editText可输入的最大的长度
int maxLength = typedArray.getInt(R.styleable.EditLayout_maxLength, 25);
editInput.setMaxLines(maxLength);
break;
default:
break;
}
}
addView(view);
typedArray.recycle();
}
public EditInfoLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public String getInputInfo(){
String info = "";
info = editInput.getText().toString();
return info;
}
}
四、在布局文件中使用该控件
(注意在xml文件顶部定义命名空间xmlns)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:uview="http://schemas.android.com/apk/res/com.daxin.daxinpay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:fitsSystemWindows="true"
tools:context="${relativePackage}.${activityClass}" >
<!--inputType 2:number; 3:phone; 12:password -->
<com.daxin.daxinpay.view.EditInfoLayout
android:id="@+id/layout_old_login_password"
style="@style/dx_wm_hw"
uview:editLable="@string/modify_old_password"
uview:editHint="@string/set_pay_please_input_password"
uview:inputType="12"
uview:maxLength="25"
android:layout_marginTop="10dp"/>
<com.daxin.daxinpay.view.EditInfoLayout
android:id="@+id/layout_new_login_password"
style="@style/dx_wm_hw"
uview:editLable="@string/password"
uview:editHint="@string/please_input_new_password"
uview:inputType="12"
uview:maxLength="25"
android:layout_below="@id/layout_old_login_password"/>
<com.daxin.daxinpay.view.EditInfoLayout
android:id="@+id/layout_ensure_new_login_password"
style="@style/dx_wm_hw"
uview:editLable="@string/set_password_ensure"
uview:editHint="@string/please_ensure_new_password"
uview:inputType="12"
uview:maxLength="25"
android:layout_below="@id/layout_new_login_password"/>
<com.daxin.daxinpay.view.DXButton
android:id="@+id/btn_set_login_password_next"
style="@style/dx_button"
android:layout_margin="15dp"
android:text="@string/commit"
android:layout_below="@id/layout_ensure_new_login_password"/>
</RelativeLayout>