Android 基本事件及对话框

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) { //多选项和按钮对话框

                   /* 点击复选框的响应 */

                   }

 })

原文地址:https://www.cnblogs.com/oftenlin/p/2984674.html