qq账号输入框
主要过程
1.创建一个ClearEditText类
2.写ClearEditText代码(直接复制粘贴,第一行注意改就行)
//自己创类的地址 如我的:package com.example.qqwanzhengban.function;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
public class ClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher {
private Drawable mClearDrawable;
private boolean hasFocus;
public ClearEditText(Context context) {
this(context, null);
}
public ClearEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
// getCompoundDrawables() Returns drawables for the left(0), top(1), right(2) and bottom(3)
mClearDrawable = getCompoundDrawables()[2]; // 获取drawableRight
if (mClearDrawable == null) {
// 如果为空,即没有设置drawableRight,则使用R.mipmap.close这张图片
mClearDrawable = getResources().getDrawable(R.mipmap.close);
}
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
setOnFocusChangeListener(this);
addTextChangedListener(this);
// 默认隐藏图标
setDrawableVisible(false);
}
/**
* 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟clear点击事件
* 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
int start = getWidth() - getTotalPaddingRight() + getPaddingRight(); // 起始位置
int end = getWidth(); // 结束位置
boolean available = (event.getX() > start) && (event.getX() < end);
if (available) {
this.setText("");
}
}
}
return super.onTouchEvent(event);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
this.hasFocus = hasFocus;
if (hasFocus && getText().length() > 0) {
setDrawableVisible(true); // 有焦点且有文字时显示图标
} else {
setDrawableVisible(false);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
if (hasFocus) {
setDrawableVisible(s.length() > 0);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
protected void setDrawableVisible(boolean visible) {
Drawable right = visible ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:clearEditText="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/home1"
>
<LinearLayout
android:id="@+id/form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.qqwanzhengban.function.ClearEditText
android:id="@+id/playername"
android:layout_centerHorizontal="true"
android:layout_width="280dp"
android:layout_height="50dp"
android:inputType="number"
android:hint="输入账号名" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.qqwanzhengban.function.ClearEditText
android:id="@+id/playermm"
android:layout_width="280dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:hint="输入密码"
android:inputType="textPassword" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
>
<Button
android:layout_width="180dp"
android:layout_height="40dp"
android:background="#03A9F4"
android:text="登录"
android:textColor="@color/white"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:text="忘记密码?"
android:textColor="#03A9F4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:background="#00000000"
android:text="新用户注册"
android:layout_alignParentRight="true"
android:textColor="#03A9F4" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50px"
>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录及代表阅读并同意"
android:textColor="@color/white" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:text="服务条款"
android:textColor="#03A9F4" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
主要代码:
<com.example.qqwanzhengban.function.ClearEditText//输入自己ClearEditText的位置,如过上面步骤没错,输入ClearEditText会自动弹出提示
android:id="@+id/playermm"
android:layout_width="280dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:hint="输入密码"
android:inputType="textPassword" />
close.png
补充(下面为更改部分设置):
更改 close.png大小 :
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(),
mClearDrawable.getIntrinsicHeight());//设置的是原图大小(在ClearEditText中)
改50*50大小:
mClearDrawable.setBounds(0, 0, 50, 50);
为上下左右添图标:
在布局对应的Java代码内:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
setabc();
}
public void setabc(){
EditText editText=findViewById(R.id.playername);
EditText editText1=findViewById(R.id.playermm);
//用户名
Drawable left=getResources().getDrawable(R.mipmap.player);//加图像
left.setBounds(0,0,50,50);//大小
editText.setCompoundDrawables(left,null,null,null);
//密码
Drawable left1=getResources().getDrawable(R.mipmap.mm);//加图像
left1.setBounds(0,0,50,50);
editText1.setCompoundDrawables(left1,null,null,null);
}
}
主要代码:
public void setabc(){
EditText editText=findViewById(R.id.playername);
EditText editText1=findViewById(R.id.playermm);
//用户名
Drawable left=getResources().getDrawable(R.mipmap.player);//加图像
left.setBounds(0,0,50,50);//大小
editText.setCompoundDrawables(left,null,null,null);
//密码
Drawable left1=getResources().getDrawable(R.mipmap.mm);//加图像
left1.setBounds(0,0,50,50);
editText1.setCompoundDrawables(left1,null,null,null);
}
通过setCompoundDrawables(左,上,右,下)添加图片