Android OntouchListener工作不正常
我试图应用onTouchListener,但我遇到了一些代码问题,没有switchcase它可以工作,当应用switchcase它不能工作时,下面是我的代码, 开关箱代码如下Android OntouchListener工作不正常,android,Android,我试图应用onTouchListener,但我遇到了一些代码问题,没有switchcase它可以工作,当应用switchcase它不能工作时,下面是我的代码, 开关箱代码如下 if (phoneNo != null && !phoneNo.equals("") && !phoneNo.equalsIgnoreCase("null")) { textPhone.setText(phoneNo)
if (phoneNo != null && !phoneNo.equals("")
&& !phoneNo.equalsIgnoreCase("null")) {
textPhone.setText(phoneNo);
textPhone.setVisibility(View.VISIBLE);
phImage.setVisibility(View.VISIBLE);
phImage.setImageResource(R.drawable.phone);
phImage.setTag(phoneNo);
phImage.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
String phone = (String) ((ImageView) v).getTag();
Log.d(TAG, "onTouch phone--" + phone);
utils.dailPhone(v.getContext(), phone);
return false;
}
}}
else {
phImage.setVisibility(View.GONE);
textPhone.setVisibility(View.GONE);
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return false;
}
下面没有swithcase
phImage.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
String phone = (String) ((ImageView) v).getTag();
Log.d(TAG, "onTouch phone--" + phone);
utils.dailPhone(v.getContext(), phone);
return false;
}
});
} else {
phImage.setVisibility(View.GONE);
textPhone.setVisibility(View.GONE);
}
您的switch-case语法完全错误。尝试以下方法:
public boolean onTouch(MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
// finger touches the screen
String phone = (String) ((ImageView) v).getTag();
Log.d(TAG, "onTouch phone--" + phone);
utils.dailPhone(v.getContext(), phone);
return false;
break;
case MotionEvent.ACTION_MOVE:
// finger moves on the screen
break;
case MotionEvent.ACTION_UP:
// finger leaves the screen
break;
}
// tell the system that we handled the event and no further processing is required
return true;
}
@蒂基布,嗯,是吗?这只是代码。你可以任意修改它,这是我面临的问题。修改代码会在eclipse中出现一些错误。那么你可能是以错误的方式修改它。你会犯什么错误?@teekib如果你不能把这个好东西放在这里,你可能应该在来到安卓之前学习Java。