我的代码中有两个OnTouchListener,每次运行它 - 即使我更改了监听器的名称 - 它也不起作用。
//here is one
public boolean onTouch(View view, MotionEvent event){
// this is for being able to move a spawned image with no id and id not on the activity till created
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
if (no == 1) ;
//do nothing
else {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
_root.invalidate();
return true;
}
return true;
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(" exit")
.setMessage("Are you sure you want to exit?." +
"your progress will not be saved")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(new Intent(getApplicationContext(), level.class));
}
})
.setNegativeButton("No", null)
.show();
}
Button left, right;
ImageView i1, pic2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level1_game);
_root = (ViewGroup) findViewById(R.id.root);
addnumbers = new TextView(this);
addnumbers.setText(Integer.toString(number));
_root.addView(addnumbers);
left = (Button) findViewById(R.id.button1);
right = (Button) findViewById(R.id.button2);
left.setOnTouchListener(this);
right.setOnTouchListener(this);
i1 = (ImageView) findViewById(R.id.imageView2);
findViewById(R.id.button2).setVisibility(View.INVISIBLE);
findViewById(R.id.button1).setVisibility(View.INVISIBLE);
findViewById(R.id.button3).setVisibility(View.INVISIBLE);
findViewById(R.id.button4).setVisibility(View.INVISIBLE);
findViewById(R.id.imageView2).setVisibility(View.INVISIBLE);
}
这是第二个在屏幕上移动角色的方法
public boolean onTouch1(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.button1:
case MotionEvent.ACTION_DOWN: {
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
i1.getLayoutParams();
findViewById(R.id.imageView2).setBackgroundDrawable(getResources().getDrawable(R.drawable.realguy1));
mParams.leftMargin -= 20;
i1.setLayoutParams(mParams);
break;
}
}
当我运行此代码时,具有OnTouch侦听器的代码可以正常工作,但另一个代码则不然。
我做了很多研究,我尝试的一切都没有用。
答案 0 :(得分:3)
我有两个View.OnTouchListeners
实际上,只有一个,这就是为什么......
onTouch1
对SDK没有任何意义。你所做的只是定义一个方法。您的编译器甚至可能会告诉您该方法未使用。
这两个都是onTouch
,因为这里暗示了this
的含义。
left.setOnTouchListener(this);
right.setOnTouchListener(this);
因此, this
是唯一View.OnTouchListener
,而且它唯一的方法是 onTouch
。
如果您正确地注释了您的方法,那么您就会看到它。例如,
@Override // This is okay
public boolean onTouch(View view, MotionEvent event){
}
@Override // Not okay, nothing to override
public boolean onTouch1(View view, MotionEvent event){
}
现在,你可以做这样的事情......
使用仅限onTouch
方法,并在每个按钮上添加switch (view.getId())
case
。
@Override
public boolean onTouch(View view, MotionEvent event){
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (view.getId()) {
case R.id.button1:
// left
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// ...
}
return true;
case R.id.button2:
// right
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// ...
return true;
}
return false;
}
当然,您决定将其重构为您认为合适的多种单独方法。
例如,如果您想保留onTouch1
,可以直接将onTouch
委托给它。
@Override
public boolean onTouch(View view, MotionEvent event){
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (view.getId()) {
case R.id.button1:
// left
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// ...
}
return true;
case R.id.button2:
// right
return onTouch1(view, event); // delegate to other method
}
return false;
}
此外,永远不应使用View ID在交换机上触发case MotionEvent.ACTION_DOWN:
,因此您应将其删除
switch (v.getId()) {
case R.id.button1:
case MotionEvent.ACTION_DOWN: // Shouldn't work