我似乎无法识别 android 上的触摸事件

I can't seem to recognize touch events on android

我只是想检测每次点击屏幕时经过了多少时间。我似乎无法识别屏幕点击。我有一个应该调用我的 checkTime 方法的 onTouchEvent 方法。 toast 没有显示任何内容,System.out.println 没有打印。

package com.example.biastester;

import java.util.Calendar;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

public class Test extends ActionBarActivity {

Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);



}

public boolean onTouchEvent(MotionEvent event){
    checkTime();
    return true;
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


public void checkTime(){
    int currSeconds = c.get(Calendar.SECOND);
    Toast.makeText(this, "tapped", 2);
    System.out.println("the time elapsed since the start of this test is" + (currSeconds-seconds) );
    if(currSeconds-seconds > 120){
        Intent intent = new Intent(this, Results.class);
        startActivity(intent);
    }
}

}

我以前从未使用过 android,我不知道自己做错了什么。预先感谢您的帮助。

您需要捕获点击事件

此代码处理 increate 中的点击

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    Button b = new Button(this);

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkTime();
        }
    });

    setContentView(new Button(this));

}