Android学习--keyboardEvent
来源:互联网 发布:淘宝联盟怎么自推自买 编辑:程序博客网 时间:2024/06/06 21:32
键盘事件
打开android-sdk/docs/reference/index.html
在侧边栏找到android.app,点击它
在右侧Classes找到Activity,点击
进入Activity类之后你会找到它有一个方法onKeyDown,这就是键盘(手机键盘)事件处理的方法。
创建工程TestKeyboardEvent,工程目录如下
activity_test_keyboard_event.xml 代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/alphValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <ImageViewandroid:id = "@+id/image" android:src="@drawable/ic_launcher" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
android:src="@drawable/ic_launcher"这个drawable的值的问题,在R.java中存在资源ic_launcher,同时在drawable...文件夹中可以找到图片ic_launcher.png
TestKeyboardEvent.java 代码如下:
package com.hb.yooge.testkeyboardevent;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.KeyEvent;import android.view.Menu;import android.widget.ImageView;import android.widget.TextView;public class TestKeyboardEvent extends Activity {private static final String TAG = "TestKeyBoardEvent";private ImageView mImage;private TextView mAlphValueText;private int mAlphValue;private void findViewsById(){mImage = (ImageView) findViewById(R.id.image);mAlphValueText = (TextView) findViewById(R.id.alphValue);} @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_keyboard_event); findViewsById(); mAlphValue = 100; mImage.setAlpha(mAlphValue); mAlphValueText.setText("Alpha = "+mAlphValue*100/0xff+"%"); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { Log.v(TAG, "onKeyDown: keyCode = " + keyCode); Log.v(TAG, "onKeyDown: string = " + event.toString()); switch(keyCode){ case KeyEvent.KEYCODE_DPAD_UP: ; case KeyEvent.KEYCODE_DPAD_LEFT: mAlphValue += 20; break; case KeyEvent.KEYCODE_DPAD_DOWN: ; case KeyEvent.KEYCODE_DPAD_RIGHT: mAlphValue -= 20; break; default: System.out.println("============================================"); } if(mAlphValue >= 0xff){ mAlphValue = 0xff; } if(mAlphValue <= 0){ mAlphValue = 0; } mImage.setAlpha(mAlphValue); mAlphValueText.setText("Alpha = " + mAlphValue + "%"); return super.onKeyDown(keyCode, event); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_test_keyboard_event, menu); return true; }}
R.java 代码如下:
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */package com.hb.yooge.testkeyboardevent;public final class R { public static final class attr { } public static final class drawable { public static final int ic_action_search=0x7f020000; public static final int ic_launcher=0x7f020001; } public static final class id { public static final int alphValue=0x7f070001; public static final int image=0x7f070002; public static final int menu_settings=0x7f070003; public static final int screen=0x7f070000; } public static final class layout { public static final int activity_test_keyboard_event=0x7f030000; } public static final class menu { public static final int activity_test_keyboard_event=0x7f060000; } public static final class string { public static final int app_name=0x7f040000; public static final int hello_world=0x7f040001; public static final int menu_settings=0x7f040002; public static final int title_activity_test_keyboard_event=0x7f040003; } public static final class style { public static final int AppTheme=0x7f050000; }}
运行结果:按下左右可以看到图片的Alpha值改变了,同时图片变蒙了
- Android学习--keyboardEvent
- Flex KeyboardEvent Code
- page 13 KeyBoardEvent
- android学习
- Android学习
- android 学习
- Android学习
- android 学习
- 学习android
- android学习
- Android学习
- android学习
- android学习
- android学习
- Android 学习
- 学习Android
- android学习
- Android学习
- 统一编址(51系列)
- 计算机视觉、模式识别、机器学习常用牛人主页链接
- Object-C笔记
- 复制字符串str1,保存在str2中
- 关于window.external
- Android学习--keyboardEvent
- 面片上显示3D模型
- 16-2 指针型成绩单
- 偶然看到一个关于swing布局管理器的总结,觉得不错,如下。
- C++ sizeof 使用规则及陷阱分析
- nagios 监控Linux和Unix服务器(ubuntu服务器安装实例)
- 今天出现这个错误:'sessionFactory' or 'hibernateTemplate' is required
- oracle sqlplus autotrace功能
- OSLO由渐晕设置孔径的源码分析