Skip to content

Commit

Permalink
V1.52 优化交互
Browse files Browse the repository at this point in the history
  • Loading branch information
LGH1996 committed Nov 30, 2024
1 parent a69427b commit c64991e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 5 deletions.
109 changes: 109 additions & 0 deletions app/src/main/java/com/lgh/tapclick/myview/MyEditTextView.java
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;
}
}
}
}
Binary file added app/src/main/res/drawable/text_clean_btn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions app/src/main/res/layout/view_widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
android:textColor="@color/text_color"
android:textSize="12sp" />

<androidx.appcompat.widget.AppCompatEditText
<com.lgh.tapclick.myview.MyEditTextView
android:id="@+id/widget_rect"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -96,7 +96,7 @@
android:textColor="@color/text_color"
android:textSize="12sp" />

<androidx.appcompat.widget.AppCompatEditText
<com.lgh.tapclick.myview.MyEditTextView
android:id="@+id/widget_node_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -121,7 +121,7 @@
android:textColor="@color/text_color"
android:textSize="12sp" />

<androidx.appcompat.widget.AppCompatEditText
<com.lgh.tapclick.myview.MyEditTextView
android:id="@+id/widget_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -146,7 +146,7 @@
android:textColor="@color/text_color"
android:textSize="12sp" />

<androidx.appcompat.widget.AppCompatEditText
<com.lgh.tapclick.myview.MyEditTextView
android:id="@+id/widget_describe"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -171,7 +171,7 @@
android:textColor="@color/text_color"
android:textSize="12sp" />

<androidx.appcompat.widget.AppCompatEditText
<com.lgh.tapclick.myview.MyEditTextView
android:id="@+id/widget_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down

0 comments on commit c64991e

Please sign in to comment.