我需要在像 ms word screen 这样的编辑文本中添加一个可触摸的图像视图。我们如何为此目的设计一个 android 布局屏幕?我尝试了下面显示的代码:
public class edittext extends EditText
{
public String defaultValue = "";
final Drawable imgX = getResources().getDrawable(android.R.drawable.presence_offline ); // X image
private Html.ImageGetter imageGetter;
public edittext(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
// TODO Auto-generated constructor stub
}
public edittext(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
// TODO Auto-generated constructor stub
}
public edittext(Context context)
{
super(context);
init();
// TODO Auto-generated constructor stub
}
void init() {
// Set bounds of our X button
imgX.setBounds(0, 0, imgX.getIntrinsicWidth(), imgX.getIntrinsicHeight());
// There may be initial text in the field, so we may need to display the button
manageClearButton();
edittext.this.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
edittext et = edittext.this;
// Is there an X showing?
if (et.getCompoundDrawables()[2] == null) return false;
// Only do this for up touches
if (event.getAction() != MotionEvent.ACTION_UP) return false;
// Is touch on our clear button?
if (event.getX() > et.getWidth() - et.getPaddingRight() - imgX.getIntrinsicWidth()) {
et.setText("");
edittext.this.removeClearButton();
}
return false;
}
});
edittext.this.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
edittext.this.manageClearButton();
}
public void afterTextChanged(Editable arg0) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
}
void manageClearButton() {
if (this.getText().toString().equals("") )
removeClearButton();
else
addClearButton();
}
void addClearButton() {
this.setCompoundDrawables(this.getCompoundDrawables()[0],
this.getCompoundDrawables()[1],
imgX,
this.getCompoundDrawables()[3]);
}
void removeClearButton() {
this.setCompoundDrawables(this.getCompoundDrawables()[0],
this.getCompoundDrawables()[1],
null,
this.getCompoundDrawables()[3]);
}
}
如果有人知道,请帮助我谢谢。