The clearance comes EditText
-
Renderings
Code
/**
* Created on 2019/6/5 10:12 AM
*
* @author GYQ
*/
@SuppressLint("AppCompatCustomView")
public class ClearEditText extends EditText implements View.OnFocusChangeListener, TextWatcher {
private Drawable mClearDrawable;
private boolean hasFocus;
public ClearEditText(Context context) {
this(context, null);
}
public ClearEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mClearDrawable = getCompoundDrawables()[2];
if (mClearDrawable == null) {
mClearDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_clear,null);
}
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
setOnFocusChangeListener(this);
addTextChangedListener(this);
// 默认隐藏图标
setDrawableVisible(false);
}
/**
* 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟clear点击事件
* 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
int start = getWidth() - getTotalPaddingRight() + getPaddingRight();
int end = getWidth();
boolean available = (event.getX() > start) && (event.getX() < end);
if (available) {
this.setText("");
}
}
}
return super.onTouchEvent(event);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
this.hasFocus = hasFocus;
if (hasFocus && getText().length() > 0) {
setDrawableVisible(true);
} else {
setDrawableVisible(false);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
if (hasFocus) {
setDrawableVisible(s.length() > 0);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
protected void setDrawableVisible(boolean visible) {
Drawable right = visible ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
}
- Simple to use
<com.scarf.test.ClearEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Comes with pull-down function EditText
-
Renderings
Code
/**
* Created on 2019/6/5 10:31 AM
*
* @author Scarf Gong
*/
@SuppressLint("AppCompatCustomView")
public class DropEditText extends EditText implements PopupWindow.OnDismissListener, AdapterView.OnItemClickListener {
private Drawable mDrawable;
private PopupWindow mPopupWindow;
private ListView mPopListView;
private int mDropDrawableResId;
private int mRiseDrawableResID;
public DropEditText(Context context) {
this(context, null);
}
public DropEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DropEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
mPopListView = new ListView(context);
mDropDrawableResId = R.drawable.ic_drop_down;
mRiseDrawableResID = R.drawable.ic_drop_up;
showDropDrawable(); // 默认显示下拉图标
mPopListView.setOnItemClickListener(this);
}
/**
* 我们无法直接给EditText设置点击事件,只能通过按下的位置来模拟点击事件
* 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (getCompoundDrawables()[2] != null) {
int start = getWidth() - getTotalPaddingRight() + getPaddingRight();
int end = getWidth();
boolean available = (event.getX() > start) && (event.getX() < end);
if (available) {
closeSoftInput();
showPopWindow();
return true;
}
}
}
return super.onTouchEvent(event);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (changed) {
mPopupWindow = new PopupWindow(mPopListView, getWidth(), LinearLayout.LayoutParams.WRAP_CONTENT);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
mPopupWindow.setFocusable(true);
mPopupWindow.setOnDismissListener(this);
}
}
private void showPopWindow() {
mPopupWindow.showAsDropDown(this, 0, 5);
showRiseDrawable();
}
private void showDropDrawable() {
mDrawable = getResources().getDrawable(mDropDrawableResId);
mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], mDrawable, getCompoundDrawables()[3]);
}
private void showRiseDrawable() {
mDrawable = getResources().getDrawable(mRiseDrawableResID);
mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], mDrawable, getCompoundDrawables()[3]);
}
public void setAdapter(BaseAdapter adapter) {
mPopListView.setAdapter(adapter);
}
private void closeSoftInput() {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getWindowToken(), 0);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
this.setText(mPopListView.getAdapter().getItem(position).toString());
mPopupWindow.dismiss();
}
@Override
public void onDismiss() {
showDropDrawable();
}
}
- Simple to use
private void init() {
dropEditText = (DropEditText) findViewById(R.id.drop_edit_text);
String[] strings = new String[10];
for (int i = 0; i < 10; i++) {
strings[i] = "美女" + i + "号";
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
dropEditText.setAdapter(adapter);
}
Enter into the soft keyboard search button, etc.
- Add imeOptions property:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
android:singleLine="true"/>
Note: there must also set singLine = "true", otherwise the function of a carriage return or line feed.
- Common attributes
actionNext Next, typically used to jump to the next EditText
actionGo go, usually used to open links
actionSend transmission, typically for transmitting information
actionSearch search, typically used to search for information
actionDone confirmed, usually it means things done
EditText enter the phone number comes spaces
EditText enter the phone number automatically with spaces
Comes icon EditText
Reproduced in: https: //www.jianshu.com/p/3b0ab4985223