|
@@ -10,9 +10,9 @@ import android.text.TextWatcher;
|
|
|
import android.util.AttributeSet;
|
|
import android.util.AttributeSet;
|
|
|
import android.view.MotionEvent;
|
|
import android.view.MotionEvent;
|
|
|
import android.view.View;
|
|
import android.view.View;
|
|
|
-
|
|
|
|
|
import com.uas.gdstorage.R;
|
|
import com.uas.gdstorage.R;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* @注释:输入框右边的清除按钮
|
|
* @注释:输入框右边的清除按钮
|
|
|
*/
|
|
*/
|
|
@@ -22,6 +22,7 @@ public class ClearableEditText extends AppCompatEditText implements View.OnTouch
|
|
|
private OnFocusChangeListener mOnFocusChangeListener;
|
|
private OnFocusChangeListener mOnFocusChangeListener;
|
|
|
private OnTouchListener mOnTouchListener;
|
|
private OnTouchListener mOnTouchListener;
|
|
|
Context context;
|
|
Context context;
|
|
|
|
|
+ private boolean isClearVisiable = false;
|
|
|
|
|
|
|
|
public ClearableEditText(Context context) {
|
|
public ClearableEditText(Context context) {
|
|
|
super(context);
|
|
super(context);
|
|
@@ -38,10 +39,15 @@ public class ClearableEditText extends AppCompatEditText implements View.OnTouch
|
|
|
init(context);
|
|
init(context);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void init(Context pContext){
|
|
|
|
|
|
|
+ public void setClearVisiable(boolean clearVisiable) {
|
|
|
|
|
+ isClearVisiable = clearVisiable;
|
|
|
|
|
+ setClearIconVisible(clearVisiable);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void init(Context pContext) {
|
|
|
context = pContext;
|
|
context = pContext;
|
|
|
//封装drawable对象,系统默认图标:R.drawable.abc_ic_clear_mtrl_alpha
|
|
//封装drawable对象,系统默认图标:R.drawable.abc_ic_clear_mtrl_alpha
|
|
|
- Drawable drawable = ContextCompat.getDrawable(context, R.drawable.wrong );
|
|
|
|
|
|
|
+ Drawable drawable = ContextCompat.getDrawable(context, R.drawable.wrong);
|
|
|
Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
|
|
Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
|
|
|
//简单着色
|
|
//简单着色
|
|
|
DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
|
|
DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
|
|
@@ -59,18 +65,19 @@ public class ClearableEditText extends AppCompatEditText implements View.OnTouch
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void setClearIconVisible(final boolean visible) {
|
|
private void setClearIconVisible(final boolean visible) {
|
|
|
- mClearTextIcon.setVisible(visible, false);
|
|
|
|
|
|
|
+ boolean isVisible = isClearVisiable || visible;
|
|
|
|
|
+ mClearTextIcon.setVisible(isVisible, false);
|
|
|
final Drawable[] compoundDrawables = getCompoundDrawables();
|
|
final Drawable[] compoundDrawables = getCompoundDrawables();
|
|
|
//在edittext右侧设置图标
|
|
//在edittext右侧设置图标
|
|
|
setCompoundDrawables(
|
|
setCompoundDrawables(
|
|
|
compoundDrawables[0],
|
|
compoundDrawables[0],
|
|
|
compoundDrawables[1],
|
|
compoundDrawables[1],
|
|
|
- visible ? mClearTextIcon : null,
|
|
|
|
|
|
|
+ isVisible ? mClearTextIcon : null,
|
|
|
compoundDrawables[3]);
|
|
compoundDrawables[3]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//设置警告标志可见
|
|
//设置警告标志可见
|
|
|
- public void setWarnIconVisible(){
|
|
|
|
|
|
|
+ public void setWarnIconVisible() {
|
|
|
Drawable warnDrawable = ContextCompat.getDrawable(context, R.drawable.warn);
|
|
Drawable warnDrawable = ContextCompat.getDrawable(context, R.drawable.warn);
|
|
|
Drawable wrapWarnDrawable = DrawableCompat.wrap(warnDrawable);
|
|
Drawable wrapWarnDrawable = DrawableCompat.wrap(warnDrawable);
|
|
|
warnDrawable = wrapWarnDrawable;
|
|
warnDrawable = wrapWarnDrawable;
|
|
@@ -88,15 +95,17 @@ public class ClearableEditText extends AppCompatEditText implements View.OnTouch
|
|
|
compoundDrawables[3]);
|
|
compoundDrawables[3]);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
//设置警告标志可见
|
|
//设置警告标志可见
|
|
|
- public void setSmallWarnIconVisible(){
|
|
|
|
|
|
|
+ public void setSmallWarnIconVisible() {
|
|
|
Drawable warnDrawable = ContextCompat.getDrawable(context, R.drawable.warn);
|
|
Drawable warnDrawable = ContextCompat.getDrawable(context, R.drawable.warn);
|
|
|
Drawable wrapWarnDrawable = DrawableCompat.wrap(warnDrawable);
|
|
Drawable wrapWarnDrawable = DrawableCompat.wrap(warnDrawable);
|
|
|
warnDrawable = wrapWarnDrawable;
|
|
warnDrawable = wrapWarnDrawable;
|
|
|
int f = context.getResources().getDimensionPixelSize(R.dimen.hint_pic_small);
|
|
int f = context.getResources().getDimensionPixelSize(R.dimen.hint_pic_small);
|
|
|
warnDrawable.setBounds(0, 0, f, f);
|
|
warnDrawable.setBounds(0, 0, f, f);
|
|
|
|
|
|
|
|
- final Drawable[] compoundDrawables = getCompoundDrawables(); mClearTextIcon.setBounds(0, 0, mClearTextIcon.getIntrinsicHeight(), mClearTextIcon.getIntrinsicWidth());
|
|
|
|
|
|
|
+ final Drawable[] compoundDrawables = getCompoundDrawables();
|
|
|
|
|
+ mClearTextIcon.setBounds(0, 0, mClearTextIcon.getIntrinsicHeight(), mClearTextIcon.getIntrinsicWidth());
|
|
|
|
|
|
|
|
//在edittext右侧设置图标
|
|
//在edittext右侧设置图标
|
|
|
setCompoundDrawables(
|
|
setCompoundDrawables(
|
|
@@ -106,7 +115,8 @@ public class ClearableEditText extends AppCompatEditText implements View.OnTouch
|
|
|
compoundDrawables[3]);
|
|
compoundDrawables[3]);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
- private void dismissOkIcon(final boolean visible){
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private void dismissOkIcon(final boolean visible) {
|
|
|
mClearTextIcon.setVisible(visible, false);
|
|
mClearTextIcon.setVisible(visible, false);
|
|
|
final Drawable[] compoundDrawables = getCompoundDrawables();
|
|
final Drawable[] compoundDrawables = getCompoundDrawables();
|
|
|
//在edittext右侧设置图标
|
|
//在edittext右侧设置图标
|
|
@@ -118,8 +128,9 @@ public class ClearableEditText extends AppCompatEditText implements View.OnTouch
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
boolean isOk = false;
|
|
boolean isOk = false;
|
|
|
|
|
+
|
|
|
//设置成功标志可见
|
|
//设置成功标志可见
|
|
|
- public void setOkIconVisible(){
|
|
|
|
|
|
|
+ public void setOkIconVisible() {
|
|
|
isOk = true;
|
|
isOk = true;
|
|
|
Drawable warnDrawable = ContextCompat.getDrawable(context, R.drawable.ok);
|
|
Drawable warnDrawable = ContextCompat.getDrawable(context, R.drawable.ok);
|
|
|
Drawable wrapWarnDrawable = DrawableCompat.wrap(warnDrawable);
|
|
Drawable wrapWarnDrawable = DrawableCompat.wrap(warnDrawable);
|
|
@@ -142,14 +153,13 @@ public class ClearableEditText extends AppCompatEditText implements View.OnTouch
|
|
|
public void onFocusChange(View v, boolean hasFocus) {
|
|
public void onFocusChange(View v, boolean hasFocus) {
|
|
|
if (hasFocus) {
|
|
if (hasFocus) {
|
|
|
setClearIconVisible(getText().length() > 0);
|
|
setClearIconVisible(getText().length() > 0);
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
|
|
+ } else {
|
|
|
//如果不是正确图片,则焦点消失清空图片
|
|
//如果不是正确图片,则焦点消失清空图片
|
|
|
- if(!isOk){
|
|
|
|
|
|
|
+ if (!isOk) {
|
|
|
setClearIconVisible(false);
|
|
setClearIconVisible(false);
|
|
|
}
|
|
}
|
|
|
//如果是正确图片,则焦点消失后图片也不消失,显示正确图片
|
|
//如果是正确图片,则焦点消失后图片也不消失,显示正确图片
|
|
|
- else{
|
|
|
|
|
|
|
+ else {
|
|
|
setOkIconVisible();
|
|
setOkIconVisible();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -160,12 +170,14 @@ public class ClearableEditText extends AppCompatEditText implements View.OnTouch
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 输入框获取焦点*/
|
|
|
|
|
- private void editTextGetFocus(){
|
|
|
|
|
|
|
+ * 输入框获取焦点
|
|
|
|
|
+ */
|
|
|
|
|
+ private void editTextGetFocus() {
|
|
|
setFocusable(true);
|
|
setFocusable(true);
|
|
|
setFocusableInTouchMode(true);
|
|
setFocusableInTouchMode(true);
|
|
|
requestFocus();
|
|
requestFocus();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
final int x = (int) event.getX();
|
|
final int x = (int) event.getX();
|
|
@@ -176,7 +188,7 @@ public class ClearableEditText extends AppCompatEditText implements View.OnTouch
|
|
|
//EditText本身获得焦点
|
|
//EditText本身获得焦点
|
|
|
editTextGetFocus();
|
|
editTextGetFocus();
|
|
|
//外界btn使EditText自动获取焦点
|
|
//外界btn使EditText自动获取焦点
|
|
|
- if(etFocus!=null){
|
|
|
|
|
|
|
+ if (etFocus != null) {
|
|
|
etFocus.setFoucus();
|
|
etFocus.setFoucus();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -186,7 +198,8 @@ public class ClearableEditText extends AppCompatEditText implements View.OnTouch
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
EditTextFocus etFocus;
|
|
EditTextFocus etFocus;
|
|
|
- public interface EditTextFocus{
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public interface EditTextFocus {
|
|
|
void setFoucus();
|
|
void setFoucus();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -203,6 +216,7 @@ public class ClearableEditText extends AppCompatEditText implements View.OnTouch
|
|
|
public void afterTextChanged(Editable s) {
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public final void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
|
|
public final void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
|
|
|
//如果文字被修改,则OK消失
|
|
//如果文字被修改,则OK消失
|