本文主要是介绍安卓onkeyup onkeydown事件小记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安卓软键盘的实现:http://blog.csdn.net/hfsu0419/article/details/7924673
Activity.onKeyDown();
当某个键被按下时会触发,但不会被任何的该Activity内的任何view处理。
默认按下KEYCODE_BACK键后会回到上一个Activity。
Activity.onKeyUp():
当某个按键被按下,松开后触发,但不会被任何的该Activity内的任何view处理。
默认没有执行任何操作,只是简单的给一个false作为返回值。
在Android中是通过触屏及键盘来操作程序的,我们如何响应一般的键盘及触笔动作呢?通过对Android一些基本界面元素的操作的了解,Android将会如何处理键盘事件?仍在Activity中改事件响应函数来做到。
一般是如下三个Android键盘操作的函数:
onKeyDown, onKeyUp, on, onKeyMultiple
参看如下Android键盘操作代码:
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- public class TestProgress extends Activity {
- private ProgressDialog progress = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- menu.add(0, Menu.FIRST+1, 1, "Open Progress");
- menu.add(0, Menu.FIRST+2, 2, "Exit");
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- super.onOptionsItemSelected(item);
- switch (item.getItemId())
- {
- case Menu.FIRST +1:
- {
- progress = new ProgressDialog(this);
- progress.setTitle("Progress!!");
- progress.setMessage("Please wait for the operation...");
- progress.setCancelable(true);
- progress.show();
- //progress = ProgressDialog.show(this, "Progress!", "Please wait for operation...");
- break;
- }
- case Menu.FIRST +2:
- {
- finish();
- break;
- }
- }
- return true;
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- // TODO Auto-generated method stub
- super.onKeyDown(keyCode, event);
- setTitle("you pressed key:" + String.valueOf(keyCode));
- return true;
- }
- @Override
- public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
- // TODO Auto-generated method stub
- super.onKeyMultiple(keyCode, repeatCount, event);
- TextView tv = (TextView)this.findViewById(R.id.mainview);
- tv.setText("you have press key:[" + String.valueOf(keyCode) + "] for:" + String.valueOf(repeatCount) + "Times!");
- return true;
- }
- @Override
- public boolean onKeyUp(int keyCode, KeyEvent event) {
- // TODO Auto-generated method stub
- super.onKeyUp(keyCode, event);
- setTitle("you release key:" + String.valueOf(keyCode));
- return true;
- }
- }
这篇关于安卓onkeyup onkeydown事件小记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!