本文介绍了如何在AutoCompleteTextView中创建干净的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
中创建干净的按钮如何在AutoCompleteTextView中创建干净的按钮,当我单击清除按钮时,我想清除AutoCompleteTextView中的所有文本,请参见图片
解决方案
如
How can I create clean Button in AutoCompleteTextView, when I click clear button I want to clear all text in AutoCompleteTextView, see in picture
解决方案
As explained in this post by Michael Derazon, you can extend your AutoCompleteTextView to include a custom clear function. The implementation will look like this:
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AutoCompleteTextView;
/**
* sub class of {@link android.widget.AutoCompleteTextView} that includes a clear (dismiss / close) button with
* a OnClearListener to handle the event of clicking the button
* based on code from {@link http://www.gubed.net/clearableautocompletetextview}
* @author Michael Derazon
*
*/
public class ClearableAutoCompleteTextView extends AutoCompleteTextView {
// was the text just cleared?
boolean justCleared = false;
// if not set otherwise, the default clear listener clears the text in the
// text view
private OnClearListener defaultClearListener = new OnClearListener() {
@Override
public void onClear() {
ClearableAutoCompleteTextView et = ClearableAutoCompleteTextView.this;
et.setText("");
}
};
private OnClearListener onClearListener = defaultClearListener;
// The image we defined for the clear button
public Drawable imgClearButton = getResources().getDrawable(
R.drawable.abc_ic_clear_holo_light);
public interface OnClearListener {
void onClear();
}
/* Required methods, not used in this implementation */
public ClearableAutoCompleteTextView(Context context) {
super(context);
init();
}
/* Required methods, not used in this implementation */
public ClearableAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
/* Required methods, not used in this implementation */
public ClearableAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
void init() {
// Set the bounds of the button
this.setCompoundDrawablesWithIntrinsicBounds(null, null,
imgClearButton, null);
// if the clear button is pressed, fire up the handler. Otherwise do nothing
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ClearableAutoCompleteTextView et = ClearableAutoCompleteTextView.this;
if (et.getCompoundDrawables()[2] == null)
return false;
if (event.getAction() != MotionEvent.ACTION_UP)
return false;
if (event.getX() > et.getWidth() - et.getPaddingRight() - imgClearButton.getIntrinsicWidth()) {
onClearListener.onClear();
justCleared = true;
}
return false;
}
});
}
public void setImgClearButton(Drawable imgClearButton) {
this.imgClearButton = imgClearButton;
}
public void setOnClearListener(final OnClearListener clearListener) {
this.onClearListener = clearListener;
}
public void hideClearButton() {
this.setCompoundDrawables(null, null, null, null);
}
public void showClearButton() {
this.setCompoundDrawablesWithIntrinsicBounds(null, null, imgClearButton, null);
}
}
See the full post for a complete guide on how to implement it. The result will be as below picture:
这篇关于如何在AutoCompleteTextView中创建干净的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!