你能进来,说明你是一个刚入Android界的新人,嘿嘿,没有嘲笑你的意思,因为我也是新人;因此就有为了自己不断积累经验的责任和义务!不是吗?谁敢说天生就是天才,天生就是优秀的程序员!好,那咱们看一看效果图:
登录UI:
第一步:自定义属性(/res/values/attrs.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="delete_edittext">
<attr name="height" format="reference|dimension" />
<attr name="paddingleft" format="reference|dimension" />
<attr name="hint" format="reference|string" />
<attr name="hint_size" format="reference|dimension" />
<attr name="hint_color" format="reference|color" />
<attr name="delete_edittype" format="string" />
</declare-styleable>
</resources>
public class LoginEditText extends RelativeLayout {
//取消按钮
private ImageView cancelIm;
private EditText searchEt;
//ed的高度
private float edHieght;
//距离左边的距离
private float edPaddingleft;
//提示文字
private String hint;
private float hintSize;
private int hintColor;
//ed的类型
private String edittype;
public LoginEditText(Context context) {
super(context);
}
public LoginEditText(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context,attrs);
}
public EditText getSearchEt() {
return searchEt;
}
private void initView(Context context,AttributeSet attrs) {
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.login_edittext, this); //<span style="color:#ff0000;">这是引用了布局文件</span>
cancelIm=(ImageView) findViewById(R.id.cancel_im);
searchEt = (EditText) findViewById(R.id.search_et);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.delete_edittext);//得到属性数组
edHieght = a.getDimension(R.styleable.delete_edittext_height, 60f);//得到高度属性值,
edPaddingleft = a.getDimension(R.styleable.delete_edittext_paddingleft, 10f);//得到padding属性值,
hint = a.getString(R.styleable.delete_edittext_hint);//得到hint属性值,
hintSize = a.getDimension(R.styleable.delete_edittext_hint_size,19f);//得到hintsize属性值,
hintColor = a.getColor(R.styleable.delete_edittext_hint_color,context.getResources().getColor(R.color.c_c));//得到hintcolor属性值,
edittype = a.getString(R.styleable.delete_edittext_delete_edittype);
if("pwd".equals(edittype)) {
searchEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
searchEt.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,(int)edHieght));
searchEt.setPadding((int)edPaddingleft, 0, 0, 0);
searchEt.setHint(hint);
searchEt.setHintTextColor(hintColor);
searchEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(TextUtils.isEmpty(charSequence)) {
cancelIm.setVisibility(GONE);
} else {
cancelIm.setVisibility(VISIBLE);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
cancelIm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
searchEt.setText("");
}
});
}
public LoginEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context,attrs);
}
}
第三步:布局文件(/res/layout/login_edittext.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/head">
<EditText
android:id="@+id/search_et"
android:layout_width="fill_parent"
android:layout_height="@dimen/s_59dp"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/s_10dp"
android:layout_marginRight="@dimen/s_10dp"
android:layout_marginTop="@dimen/s_9dp"
android:background="@drawable/category_search_shape" //<span style="color:#ff0000;">这里引用了shape</span>
android:paddingLeft="@dimen/s_40dp"
android:paddingRight="@dimen/s_40dp"
android:singleLine="true"
android:textColor="@color/login_ed_hintcolor"
android:textSize="@dimen/s_17dp" />
<RelativeLayout
android:id="@+id/cancel_click"
android:layout_width="@dimen/s_35dp"
android:layout_height="@dimen/s_36dp"
android:layout_alignRight="@+id/search_et"
android:layout_centerVertical="true">
<ImageView
android:id