【发布时间】:2014-02-08 07:49:04
【问题描述】:
我正在做的是,我正在单击按钮创建编辑文本视图。我想要的是,一旦出现编辑文本视图,一个小按钮也会出现在其右侧(具有(X)图像(因此,我可以删除该特定的编辑文本视图)。我能够生成编辑文本视图,但对按钮的创建一无所知。请指导或帮助我。
【问题讨论】:
标签: android android-edittext android-button
我正在做的是,我正在单击按钮创建编辑文本视图。我想要的是,一旦出现编辑文本视图,一个小按钮也会出现在其右侧(具有(X)图像(因此,我可以删除该特定的编辑文本视图)。我能够生成编辑文本视图,但对按钮的创建一无所知。请指导或帮助我。
【问题讨论】:
标签: android android-edittext android-button
您可以简单地创建一个与 EditText 视图相同的新按钮,其中包含一个 x 并将其对齐到 EditText 字段的右侧,然后在将调用 db 的按钮上执行 onclicklistener 或任何您用于删除实际文本,然后您可以从布局中删除视图和编辑按钮。但是,从您提供的代码中,我看不到任何关于正在实现的 onClickListener 的内容。
【讨论】:
这是我的实现
<CustomEditText
android:id="@+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableRight="@drawable/ic_action_cancel"
android:ems="10"
android:focusable="true"
android:singleLine="true"
android:textColor="#000" />
其中ic_action_cancel 是x 图标。
以下是自定义的 Edittext 类
public class CustomEditText extends EditText {
private Drawable drawableRight;
int actionX, actionY;
public CustomEditText(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context) {
super(context);
}
@Override
public void setCompoundDrawables(Drawable left, Drawable top,
Drawable right, Drawable bottom) {
if (right != null) {
drawableRight = right;
}
super.setCompoundDrawables(left, top, right, bottom);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Rect bounds;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
actionX = (int) event.getX();
actionY = (int) event.getY();
// this works for left since container shares 0,0 origin with bounds
if (drawableRight != null) {
bounds = null;
bounds = drawableRight.getBounds();
int x, y;
int extraTapArea = 13;
int extraTapArea = 20;
x = (int) (actionX + extraTapArea);
y = (int) (actionY - extraTapArea);
x = getWidth() - x;
if (x <= 0) {
x += extraTapArea;
}
if (y <= 0)
y = actionY;
if (bounds.contains(x, y)) {
setText("");
return false;
}
return super.onTouchEvent(event);
}
}
return super.onTouchEvent(event);
}
}
【讨论】: