EditText onTouchListener不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EditText onTouchListener不起作用相关的知识,希望对你有一定的参考价值。
我创建了自定义的editText。它的onTouchListner根本没有调用。可能是什么原因? OnClickListener也没有调用,但addtextchangedlistener工作正常。这是代码
public class MyEditText extends EditText implements OnTouchListener{
private static final int MAX_LENGTH = 13;
String value = "";
final String viewMode = "editing";
final String viewSide = "right";
final Drawable x;
@SuppressLint("ClickableViewAccessibility")
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
x = getResources().getDrawable(R.drawable.clear);
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;
}
});
setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("=====ontouchhhh");
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;
System.out.println("=====cleared");
setText("");
setCompoundDrawables(null, null,
viewSide.equals("right") ? x3 : null, null);
}
return true;
}
});
// 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;
// #25317: 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(null, null, viewSide.equals("right") ? x2
: null, null);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("===onclick");
}
});
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(null, 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) {
}
});
}
}
答案
你的代码纠缠不清但你试试这段代码
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//your code
break;
case MotionEvent.ACTION_UP:
//your code
break;
case MotionEvent.ACTION_MOVE:
//your code
break;
default:
}
return true;
}
});
你不能实现OnTouchListener
以上是关于EditText onTouchListener不起作用的主要内容,如果未能解决你的问题,请参考以下文章
android中NestedScrollView嵌套EditText,导致滑动冲突问题