Android-自定义实现仿微信输入框输入按钮

    xiaoxiao2021-03-25  41

    自定义edittext:

    package come.zhangjie.View; 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; import come.zhangjie.R; /** * Created by kjfjkal on 2017/3/14. */ public class Edittext extends EditText implements View.OnFocusChangeListener, TextWatcher { /* 右边删除图标的引用 */ private Drawable mClearDrawable; /* 输入框是否有焦点 */ private boolean hasFocus; public Edittext(Context context) { this(context, null); } public Edittext(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.editTextStyle); } public Edittext(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { /*右边的删除图片*/ mClearDrawable = getResources().getDrawable(R.mipmap.delete_alledittext); mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); // 删除图标默认隐藏 setClearIconVisible(false); // 设置输入框焦点改变监听 setOnFocusChangeListener(this); // 设置输入框中内容发生改变监听 addTextChangedListener(this); } /** * visibletrue时,删除图标显示,当visiblefalse时,图标隐藏 * @param visible */ private void setClearIconVisible(boolean visible) { Drawable rightDrawable = visible ? mClearDrawable : null; setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], rightDrawable, getCompoundDrawables()[3]); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP: if (getCompoundDrawables()[2] != null) { boolean isClear = (event.getX() > (getWidth() - getTotalPaddingRight())) && (event.getX() < (getWidth() - getPaddingRight())); if (isClear) { setText(""); } } break; default: break; } return super.onTouchEvent(event); } /** * 当控件的焦点发生改变时会调用该方法。 * 当控件有焦点并且内容长度不为0则显示clear图标,否则隐藏。 */ @Override public void onFocusChange(View v, boolean hasFocus) { this.hasFocus = hasFocus; if(hasFocus){ setClearIconVisible(getText().length() > 0); }else{ setClearIconVisible(false); } } /** * 当控件的有内容改变时调用此方法。 * 有焦点并且内容长度不为0则显示clear图标,否则隐藏。 */ @Override public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { if(hasFocus){ setClearIconVisible(text.length() > 0); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } } XML中:

    <come.zhangjie.View.Edittext android:id="@+id/et_passwad_login" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_alignTop="@+id/imageView6" android:layout_centerHorizontal="true" android:background="@null" android:hint="输入密码" android:inputType="textPassword" android:maxLength="15" android:singleLine="true" android:textSize="15dp" android:textCursorDrawable="@null" android:drawableRight="@mipmap/delete_alledittext"/> 效果图:

    转载请注明原文地址: https://ju.6miu.com/read-50335.html

    最新回复(0)