Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1562368
  • 博文数量: 237
  • 博客积分: 5139
  • 博客等级: 大校
  • 技术积分: 2751
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-18 14:48
文章分类

全部博文(237)

文章存档

2016年(1)

2012年(4)

2011年(120)

2010年(36)

2009年(64)

2008年(12)

分类: 嵌入式

2011-06-01 10:20:54

Android UI事件处理

常见的事件处理:

boolean onKeyDown(int keyCode, KeyEvent event)用于相应按键按下;

boolean onKeyMultiple(int keyCode, KeyEvent event)用于响应按键重复点击;

boolean onKeyUp(int keyCode, KeyEvent event)用于响应按键释放

boolean onTouchEvent(MotionEvent event)用于响应触摸屏事件

直接上代码:

package com.yarin.android.Examples_04_01;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Activity01 extends Activity
{

    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button_ok = (Button) findViewById(R.id.ok);
    
        button_ok.setOnClickListener(new Button.OnClickListener() {
            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;
        }
        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_UP:
                DisplayToast("上方向键弹起");
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                DisplayToast("下方向键弹起");
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                DisplayToast("左方向键弹起");
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                DisplayToast("右方向键弹起");
                break;
        }
        
        return super.onKeyUp(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);
    }
    
    /* 显示Toast  */
    public void DisplayToast(String str)
    {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }
}

每个键都对应一个键值。可以根据需要来改变一些键的功能,这就需要自己构建KeyEvent对象,构造KeyEvent对象的

方式:

KeyEvent(int action, int code);

KeyEvent(long DownTime, long EventTime, int action, int  code, int repeat);

......

package com.yarin.android.Examples_04_02;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;

public class Activity01 extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        //构建KeyEvent对象
        KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
        return super.onKeyDown(key.getKeyCode(), key);
    }
}

 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yujixi123/archive/2010/09/16/5889145.aspx

阅读(1371) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

tianbianfei2011-07-15 13:17:04

楼主您好,本人刚学android,不知道android为什么签名啊?签名都有哪些作用?麻烦楼主到http://doumiw.com/market/community/t!showTopic.do?topicId=24这个网址帮着解答一下,行吗?谢谢了!