Activity 常用事件

onKeyDown(int keyCode, KeyEvent event):对应按键按下事件。
onKeyUp(int keyCode, KeyEvent event):对应按键松开事件。
onTouchEvent(MotionEvent event):对应点击屏幕事件。

package com.example.muzico.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    //响应,按键按下的事件
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Toast.makeText(this, "按键按下!",Toast.LENGTH_SHORT).show();
        return super.onKeyDown(keyCode,event);
    }

    //响应,按键松开的事件
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Toast.makeText(this, "按键弹起!",Toast.LENGTH_SHORT).show();
        return super.onKeyUp(keyCode,event);
    }

    //响应,屏幕触摸的事件
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        Toast.makeText(this, "点击的坐标为("+x+" : "+y+")",Toast.LENGTH_SHORT).show();
        return super.onTouchEvent(event);
    }
}