package im.weiyuan.com.viewutils; import android.content.Intent; import android.os.PersistableBundle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean dispatchKeyEvent(KeyEvent event) { Log.d("123456","activity dispatchKeyEvent is called"+event.getAction()); return super.dispatchKeyEvent(event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { Log.d("123456","activity onKeyDown is called"+event.getAction()); return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { Log.d("123456","activity onKeyUp is called"+event.getAction()); return super.onKeyUp(keyCode, event); } @Override public boolean onKeyLongPress(int keyCode, KeyEvent event) { Log.d("123456","activity onKeyLongPress is called"+event.getAction()); return super.onKeyLongPress(keyCode, event); } }
如果按键盘的返回按键:
日志的打印如下:
07-24 14:20:57.771 26412-26412/im.weiyuan.com.viewutils D/123456: activity dispatchKeyEvent is called0
07-24 14:20:57.772 26412-26412/im.weiyuan.com.viewutils D/123456: activity onKeyDown is called0
07-24 14:20:57.831 26412-26412/im.weiyuan.com.viewutils D/123456: activity dispatchKeyEvent is called1
07-24 14:20:57.831 26412-26412/im.weiyuan.com.viewutils D/123456: activity onKeyUp is called1
实现下面的一个需求:
当点击返回按钮的时候弹出一个对话框提示是否退出应用
package im.weiyuan.com.viewutils; import android.content.DialogInterface; import android.content.Intent; import android.os.PersistableBundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean dispatchKeyEvent(KeyEvent event) { Log.d("123456","activity dispatchKeyEvent is called"+event.getAction()); return super.dispatchKeyEvent(event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { Log.d("123456","activity onKeyDown is called"+event.getAction()); if(event.getAction() == KeyEvent.ACTION_DOWN){ new AlertDialog.Builder(this).setTitle("kldlskakl").setMessage(R.string.app_name) .setPositiveButton("41414", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //这里定义对话框按钮单击之后要运行的事件 } }).show(); } /** * 这里要将案件的事件消费掉,所以这里函数的返回值是true * */ return true; } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { Log.d("123456","activity onKeyUp is called"+event.getAction()); return super.onKeyUp(keyCode, event); } @Override public boolean onKeyLongPress(int keyCode, KeyEvent event) { Log.d("123456","activity onKeyLongPress is called"+event.getAction()); return super.onKeyLongPress(keyCode, event); } }