標籤:android blog http io ar os 使用 sp for
1. 在android.view.KeyEvent 中,onKeyDown表示對鍵盤按鍵的響應
需要重寫onKeyDown 函數
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg){
存在四種KeyEvent事件 KEYCODE_DPAD_UP、KEYCODE_DPAD_RIGHT、KEYCODE_DPAD_LEFT、KEYCODE_DPAD_DOWN。
2在android.view.motionEvent中,.onTouchEvent表示對觸控螢幕的響應
需要重寫onTouchEvent函數
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
3.如何?Activity 跳轉
Intent intent = new Intent(); // 建立Intent
intent.setClass(Forwarding.this, ForwardTarget.class); // 設定活動
startActivity(intent);
finish(); // 結束當前活動--可選
4.在Activity01中攜帶參數跳轉至Activity02 進行處理,並將處理結果返回給Activity01中的實現步驟
1.Activity01中指定事件中定義調轉
Intent intent = new Intent(ReceiveResult.this, SendResult.class);
startActivityForResult (intent, GET_CODE);
2.Activity02中,處理完之後將要返回的資料封裝到setAction中。
setResult(RESULT_OK, (new Intent()).setAction("Corky!"));
3.Activity01中重寫onActivityResult(int requestCode, int resultCode,Intent data)事件
5.如何添加菜單及響應
1.重寫OnCreateOptionsMenu(Menu menu)函數
2.在函數中,添加menu.add(0,RED_MENU_ID,0,r.string.red);
3.重寫public boolean onOptionsItemSelected(MenuItem item)
4.swith(item.getItemId()){
case RED_MENU_ID:
break;
}
abstract MenuItem add(int groupId, int itemId, int order, CharSequence title)
6.彈出式對話方塊
android.app.AlertDialog 來實現彈出式對話方塊
使用AlertDialog.Builder和不同的參數來構建對話方塊。
return new AlertDialog.Builder(AlertDialogSamples.this) // 返回一個對話方塊
.setIcon(R.drawable.alert_dialog_icon) // 設定icon
.setTitle(R.string.alert_dialog_two_buttons_title) // 設定title
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* 左鍵事件 */
}
});
.setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* 中鍵事件 */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* 右鍵事件 */
}
})
.setMessage(R.string.alert_dialog_two_buttons2_msg) //設定訊息內容
.setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() { //清單項目對話方塊
public void onClick(DialogInterface dialog, int which) {
String[] items =getResources().getStringArray(R.array.select_dialog_items);
new AlertDialog.Builder(AlertDialogSamples.this)
.setMessage("You selected: " + which + " , " + items[which])
.show();
}
})
.setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() { //單選項和按鈕對話方塊
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.setMultiChoiceItems(R.array.select_dialog_items3,
new boolean[]{false, true, false, true, false, false, false},
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,boolean isChecked) { //多選項和按鈕對話方塊
/* 點擊複選框的響應 */
}
})
文章引自部落格園:http://www.cnblogs.com/oftenlin/archive/2013/03/27/2984674.html
Android開發中的各類事件及彈窗實現