Skip to content

Commit c9477d0

Browse files
committed
添加焦点监听 获取焦点才展示删除按钮
1 parent 8b4d0dd commit c9477d0

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353
android:layout_marginRight="20dp"
5454
android:layout_marginTop="10dp"
5555
android:background="#ffffff"
56-
app:drawableWidth="20dp"
57-
android:textSize="14sp"
58-
app:scaleSize="0.5"/>
56+
android:textSize="14sp"/>
5957

6058
</LinearLayout>

clearedittext/src/main/java/com/cretin/www/clearedittext/view/ClearEditText.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.util.AttributeSet;
1717
import android.util.Log;
1818
import android.view.MotionEvent;
19+
import android.view.View;
1920
import android.widget.EditText;
2021

2122
import com.cretin.www.clearedittext.R;
@@ -26,7 +27,7 @@
2627
*/
2728

2829
public class ClearEditText extends EditText {
29-
private static final float DEFAUT_SCALE = 0.4f;
30+
private static final float DEFAUT_SCALE = 0.3f;
3031
private Bitmap mClearBitmap;
3132
private Paint mPaint;
3233
private int mWidth;
@@ -97,6 +98,22 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
9798
public void afterTextChanged(Editable s) {
9899
}
99100
});
101+
102+
setOnFocusChangeListener(new OnFocusChangeListener() {
103+
@Override
104+
public void onFocusChange(View v, boolean hasFocus) {
105+
if ( hasFocus ) {
106+
if ( !TextUtils.isEmpty(getText().toString()) ) {
107+
showClose = true;
108+
}else{
109+
showClose = false;
110+
}
111+
}else{
112+
showClose = false;
113+
}
114+
invalidate();
115+
}
116+
});
100117
}
101118

102119
// 处理删除事件
@@ -119,6 +136,7 @@ protected void finalize() throws Throwable {
119136
super.finalize();
120137
}
121138

139+
122140
@Override
123141
protected void onDraw(Canvas canvas) {
124142
super.onDraw(canvas);

0 commit comments

Comments
 (0)