注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

轻松度过每一天

真的猛士敢于直面惨淡的人生,敢于正视淋漓的鲜血,这是怎样的哀痛者和幸福者!!

 
 
 

日志

 
 
关于我

When you are young, you may want several love experiences. But as timegoes on, you will realize that if you really love someone, the wholelife will not be enough. You need time to know, to forgive and to love.All this needs a very big mind.

网易考拉推荐

android按键事件,KeyEvent,触摸event  

2014-12-26 09:38:13|  分类: 资料、文档 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
1.代码:Activity:

package com.vanceinfo.KeyEvent;

import android.app.Activity;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class KeyEventActivity extends Activity

{

   private Button button;

   @Override

   public void onCreate(Bundle savedInstanceState)

   {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       button = (Button) findViewById(R.id.ok_button);

       button.setOnClickListener(listener);

   }

   OnClickListener listener new OnClickListener()

   {

       @Override

       public void onClick(View v)

       {

           DisplayToast("点击了OK!");

       }

   };

   public boolean onKeyDown(int keyCode, KeyEvent event)

   {

       switch (keyCode)

       {

           case KeyEvent.KEYCODE_DPAD_CENTER:

               DisplayToast("按下:中间键");

               break;

           case KeyEvent.KEYCODE_DPAD_UP:

               DisplayToast("按下:向上键");

               break;

           case KeyEvent.KEYCODE_DPAD_DOWN:

               DisplayToast("按下:向下键");

               break;

           case KeyEvent.KEYCODE_DPAD_LEFT:

               DisplayToast("按下:左方向键");

               break;

           case KeyEvent.KEYCODE_DPAD_RIGHT:

               DisplayToast("按下:有方向键");

               break;

           default:

               break;

       }

       return super.onKeyDown(keyCode, event);

   }

   public boolean onKeyUp(int keyCode, KeyEvent event)

   {

       switch (keyCode)

       {

           case KeyEvent.KEYCODE_DPAD_CENTER:

               DisplayToast("弹起:中间键");

               break;

           case KeyEvent.KEYCODE_DPAD_DOWN:

               DisplayToast("弹起:向下键");

               break;

           case KeyEvent.KEYCODE_DPAD_LEFT:

               DisplayToast("弹起:向左键");

               break;

           case KeyEvent.KEYCODE_DPAD_RIGHT:

               DisplayToast("弹起:向右键");

               break;

           case KeyEvent.KEYCODE_DPAD_UP:

               DisplayToast("弹起:向上键");

               break;

       }

       return super.onKeyDown(keyCode, event);

   }

   public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)

   {

       return super.onKeyMultiple(keyCode, repeatCount, event);

   }

   public boolean onTouchEvent(MotionEvent event)

   {

       int iAction = event.getAction();

       if (iAction == MotionEvent.ACTION_CANCEL || iAction == MotionEvent.ACTION_DOWN

               || iAction == MotionEvent.ACTION_MOVE)

       {

           return false;

       }

       int x = (int) event.getX();

       int y = (int) event.getY();

       DisplayToast("触笔点击坐标:(" + Integer.toString(x) + "," + Integer.toString(y) + ")");

       return super.onTouchEvent(event);

   }

   public void DisplayToast(String str)

   {

       Toast.makeText(this, str, Toast.LENGTH_SHORT).show();

   }

}


效果实例:

android按键事件,KeyEvent,触摸event - 世界不属于我 - 轻松度过每一天

 android按键事件,KeyEvent,触摸event - 世界不属于我 - 轻松度过每一天 

android按键事件,KeyEvent,触摸event - 世界不属于我 - 轻松度过每一天

 

android按键事件,KeyEvent,触摸event - 世界不属于我 - 轻松度过每一天

针对想要实现某种按键效果:

Button button = (Button) this.findViewById(R.id.button);
   button.setOnTouchListener(new Button.OnTouchListener(){
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){   
                    v.setBackgroundResource(R.drawable.temp1);   
                    Log.i("TestAndroid Button", "MotionEvent.ACTION_DOWN");
                }   
                else if(event.getAction() == MotionEvent.ACTION_UP){   
                    v.setBackgroundResource(R.drawable.temp2); 
                    Log.i("TestAndroid Button", "MotionEvent.ACTION_UP");
                } 
    return false;
   }
  });

  评论这张
 
阅读(48)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018