• android KeyEvent事件机制


    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);
        }
    }
  • 相关阅读:
    两种三维点云密度聚类方法的研究与对比
    C++ string和char char*的转换
    七大排序算法总结
    map下标操作和insert区别
    c++ map 使用自定义结构做关键字
    BagFromImage安装与使用
    安装ipython notebook
    c++基本知识
    Python爬虫第一集
    91. Decode Ways
  • 原文地址:https://www.cnblogs.com/kebibuluan/p/7228869.html
Copyright © 2020-2023  润新知