onTouch无法正常工作

时间:2013-03-24 18:30:35

标签: java android ontouchlistener

好吧,所以我正在做一个简单的秒表事情,当你按下屏幕上的任何地方时,我希望它开始时间,但是现在,只需在输出中打印一些东西。我看了很多例子,我看了很多其他问题的答案,但都没有奏效。这就是我到目前为止(onTouch位于其底部):

package com.timeofcubeeliteDYLANFERRIS.cubetimerelite;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
final private int RANDOM_DIALOG = 0;
String message;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId())
    {
    case R.id.settingsMenu: 
        Intent intent = new Intent (MainActivity.this, MainPreferenceActivity.class);
        startActivity(intent);
        break;
    case R.id.cubetypeMenu:
        Intent cubeTypeIntent = new Intent (MainActivity.this, PreferenceCubeType.class);
        startActivity(cubeTypeIntent);


        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String value = preferences.getString("test", "String not found");
        System.out.println(value);
        Log.d("Cubetimer:", value);
        break;
    default:
    }
    return super.onOptionsItemSelected(item);
}






public boolean onTouch(View view, MotionEvent event){
    int action = event.getAction();
    switch(action){
    case MotionEvent.ACTION_DOWN: Log.d(message, "down"); break;
    case MotionEvent.ACTION_MOVE: Log.d(message, "move"); break;
    case MotionEvent.ACTION_UP: Log.d(message, "up"); break;

    }
    System.out.println("Oh my oh MY...");
    return true;
}

}

那么,我将如何修复触摸事件? (我不在乎保留ACTION_UP,DOWN,MOVE。这就是我试图测试的东西)

1 个答案:

答案 0 :(得分:1)

我认为你没有覆盖正确的事件。这就是为什么不调用你的方法的原因。

您应该使用onTouchEvent代替,如下所示:

@Override
public boolean onTouchEvent(MotionEvent event)
{
    //Do your stuff
    return super.onTouchEvent(event);
}