0

好的,所以我正在制作一个简单的秒表,当您按下屏幕上的任意位置时,我希望它开始计时,但现在,只需在输出中打印一些内容即可。我看过很多例子,我看了很多其他问题的答案,但没有一个有效。这是我到目前为止所拥有的(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。这正是我试图测试的)

4

1 回答 1

1

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

您应该onTouchEvent改用,如下所示:

@Override
public boolean onTouchEvent(MotionEvent event)
{
    //Do your stuff
    return super.onTouchEvent(event);
}
于 2013-03-24T18:33:31.473 回答