Android開發中的各類事件及彈窗實現

來源:互聯網
上載者:User

標籤: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開發中的各類事件及彈窗實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.