-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
5 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
app/src/main/java/com/lgh/tapclick/myview/MyEditTextView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.lgh.tapclick.myview; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.content.Context; | ||
import android.graphics.drawable.Drawable; | ||
import android.text.Editable; | ||
import android.text.TextUtils; | ||
import android.text.TextWatcher; | ||
import android.util.AttributeSet; | ||
import android.view.MotionEvent; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.appcompat.widget.AppCompatEditText; | ||
import androidx.core.content.res.ResourcesCompat; | ||
|
||
import com.lgh.tapclick.R; | ||
|
||
public class MyEditTextView extends AppCompatEditText { | ||
|
||
private Drawable cleanBtnDraw; | ||
private boolean cleanBtnDrawVisible; | ||
private boolean cleanTouchDown; | ||
private int curPaddingEnd; | ||
|
||
public MyEditTextView(@NonNull Context context) { | ||
super(context); | ||
init(); | ||
} | ||
|
||
public MyEditTextView(@NonNull Context context, @Nullable AttributeSet attrs) { | ||
super(context, attrs); | ||
init(); | ||
} | ||
|
||
public MyEditTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
init(); | ||
} | ||
|
||
private void init() { | ||
post(new Runnable() { | ||
@Override | ||
public void run() { | ||
cleanBtnDraw = ResourcesCompat.getDrawable(getResources(), R.drawable.text_clean_btn, null); | ||
float scaleRate = (float) (getHeight() - 15) / cleanBtnDraw.getIntrinsicHeight(); | ||
cleanBtnDraw.setBounds(0, 0, Math.round(cleanBtnDraw.getIntrinsicWidth() * scaleRate), getHeight() - 15); | ||
addTextChangedListener(new TextWatcher() { | ||
{ | ||
updateCleanBtnDrawState(); | ||
} | ||
|
||
@Override | ||
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | ||
|
||
} | ||
|
||
@Override | ||
public void onTextChanged(CharSequence s, int start, int before, int count) { | ||
} | ||
|
||
@Override | ||
public void afterTextChanged(Editable s) { | ||
updateCleanBtnDrawState(); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
@SuppressLint("ClickableViewAccessibility") | ||
public boolean onTouchEvent(MotionEvent event) { | ||
float right = getWidth() - curPaddingEnd; | ||
float left = right - (float) cleanBtnDraw.getBounds().right / 2; | ||
if (event.getX() >= left && event.getX() <= right) { | ||
if (event.getAction() == MotionEvent.ACTION_DOWN) { | ||
cleanTouchDown = true; | ||
} | ||
if (event.getAction() == MotionEvent.ACTION_UP) { | ||
if (cleanTouchDown) { | ||
cleanTouchDown = false; | ||
setText(null); | ||
} | ||
} | ||
return true; | ||
} | ||
cleanTouchDown = false; | ||
return super.onTouchEvent(event); | ||
} | ||
|
||
private void updateCleanBtnDrawState() { | ||
if (TextUtils.isEmpty(getText())) { | ||
if (cleanBtnDrawVisible) { | ||
setCompoundDrawables(null, null, null, null); | ||
setPadding(getPaddingLeft(), getPaddingTop(), 0, getPaddingBottom()); | ||
cleanBtnDrawVisible = false; | ||
} | ||
} else { | ||
float textWidth = getPaint().measureText(getText().toString()); | ||
curPaddingEnd = (int) Math.max(getWidth() - textWidth - cleanBtnDraw.getBounds().right, 0); | ||
setPadding(getPaddingLeft(), getPaddingTop(), curPaddingEnd, getPaddingBottom()); | ||
if (!cleanBtnDrawVisible) { | ||
setCompoundDrawables(null, null, cleanBtnDraw, null); | ||
cleanBtnDrawVisible = true; | ||
} | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters