带有叉号 (x) 的 Android 编辑文本
制作具有指定长度和删除图标的编辑文本
引言
这可能只是一个简单的自定义实现。:) 但我还是想发出来,给像我一样的 Android 编程新手。
事实上,你可能需要一个如下所示的编辑文本:
- 你的编辑文本开头有一个搜索图标。
- 当插入文本时,会显示 [X] 图标。如果点击 [X] 图标,它将删除所有输入的文本。
- 最大长度为 10 个字符。
下面的示例将解决这个问题。
Using the Code
MyEditText.java
public class MyEditText extends EditText {
private static final int MAX_LENGTH = 10;
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
this.setBackgroundResource(android.R.drawable.edit_text);
}
setOnEditorActionListener(new OnEditorActionListener() {
@Override
public synchronized boolean onEditorAction(TextView v,
int actionId, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
return true;
}
return false;
}
});
String value = "";
final String viewMode = "editing";
final String viewSide = "right";
final Drawable x = getResources().getDrawable(R.drawable.delete_icon);
// The height will be set the same with [X] icon
setHeight(x.getBounds().height());
x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight());
Drawable x2 = viewMode.equals("never") ? null : viewMode
.equals("always") ? x : viewMode.equals("editing") ? (value
.equals("") ? null : x)
: viewMode.equals("unlessEditing") ? (value.equals("") ? x
: null) : null;
// Display search icon in text field
final Drawable searchIcon = getResources().getDrawable(
android.R.drawable.ic_search_category_default);
searchIcon.setBounds(0, 0, x.getIntrinsicWidth(),
x.getIntrinsicHeight());
setCompoundDrawables(searchIcon, null, viewSide.equals("right") ? x2
: null, null);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (getCompoundDrawables()[viewSide.equals("left") ? 0 : 2] == null) {
return false;
}
if (event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
// x pressed
if ((viewSide.equals("left") && event.getX() < getPaddingLeft()
+ x.getIntrinsicWidth())
|| (viewSide.equals("right") && event.getX() > getWidth()
- getPaddingRight() - x.getIntrinsicWidth())) {
Drawable x3 = viewMode.equals("never") ? null : viewMode
.equals("always") ? x
: viewMode.equals("editing") ? null : viewMode
.equals("unlessEditing") ? x : null;
setText("");
setCompoundDrawables(searchIcon, null,
viewSide.equals("right") ? x3 : null, null);
}
return false;
}
});
addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
Drawable x4 = viewMode.equals("never") ? null : viewMode
.equals("always") ? x
: viewMode.equals("editing") ? (getText().toString()
.equals("") ? null : x) : viewMode
.equals("unlessEditing") ? (getText()
.toString().equals("") ? x : null) : null;
setCompoundDrawables(searchIcon, null,
viewSide.equals("right") ? x4 : null, null);
}
@Override
public void afterTextChanged(Editable s) {
if (s != null && s.length() > MAX_LENGTH) {
setText(s.subSequence(0, MAX_LENGTH));
setSelection(MAX_LENGTH);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
});
}
}
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.myedittext.MyEditText
android:layout_width="300dp"
android:layout_height="wrap_content" />
</RelativeLayout>
你必须在资源中创建 delete_icon.png,作为 [X] 图标,当然。
我添加了结果