Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android 界面編程#6-鍵盤操作響應

Android 界面編程#6-鍵盤操作響應

編輯:初級開發

在Android中是通過觸屏及鍵盤來操作程序的,我們如何響應一般的鍵盤及觸筆動作呢?通過對Android一些基本界面元素的操作的了解,如果你再熟悉MVC你可以猜到android將會如何處理鍵盤事件,恭喜你,猜對了,仍在Activity中改事件響應函數來做到。一般是如下三個函數:

onKeyDown, onKeyUp, on, onKeyMultiple

參看如下代碼:

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;

}

}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved