2011-10-04 71 views
0

我創建了一個警告對話框,它對我來說工作正常。現在我期望創建,如果我按軟鍵盤上的f2按鈕,我希望顯示alertbox這一切。我試過,但沒有工作。請示例代碼。如何在AlertDialog框中使用KeyEvent?

我的編碼:

public class HelloList extends Activity 
{ 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle State) { 
     super.onCreate(State); 


     setContentView(R.layout.main); 
     AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 

     // set the message to display 
     alertbox.setMessage("Disclaimer popup window sample..hai.kjidj kjdij idfjdfojodifjdojfodjfodijfoidjfoidjf kdfjdifudjf difjdfjodfjdjfodf jd fojjidjfdfjdofjdfojfodjfodjfodjfojdfojdfojdofjodjfodjfl dfjdofjdofj dfdjfodjf difjdoifj dfijdf "); 

     // set a positive/yes button and create a listener 
     alertbox.setPositiveButton("Agree/Close", new DialogInterface.OnClickListener() { 

      // do something when the button is clicked 
      public void onClick(DialogInterface dialog, int arg1) { 
       dialog.cancel(); 

      } 
     }); 

     // set a negative/no button and create a listener 
     alertbox.setNegativeButton("Buy", new DialogInterface.OnClickListener() { 

      // do something when the button is clicked 
      public void onClick(DialogInterface arg0, int arg1) { 
       arg0.cancel(); 
      } 
     }); 

      // display box 
     alertbox.show(); 
     } 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if ((keyCode == KeyEvent.KEYCODE_MENU)) { 
     //Dialog dialog = null; 
      moveTaskToBack(true); 
     //return true; 
     // dialog.dismiss(); 
     } 
     return super.onKeyDown(keyCode, event); 


    }  
} 

如果我點擊F2我WIST顯示對話框...如果在onkeydown事件的方法塊

10-04 18:57:28.936: DEBUG/dalvikvm(126): GC_EXPLICIT freed 1904 objects/110176 bytes in 131ms 
10-04 18:57:33.925: DEBUG/dalvikvm(174): GC_EXPLICIT freed 207 objects/14032 bytes in 113ms 
10-04 18:57:38.915: DEBUG/dalvikvm(266): GC_EXPLICIT freed 130 objects/6264 bytes in 98ms 
10-04 18:57:52.866: WARN/KeyCharacterMap(663): No keyboard for id 0 
10-04 18:57:52.866: WARN/KeyCharacterMap(663): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
10-04 18:57:52.886: DEBUG/AndroidRuntime(663): Shutting down VM 
10-04 18:57:52.886: WARN/dalvikvm(663): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663): FATAL EXCEPTION: main 
10-04 18:57:52.906: ERROR/AndroidRuntime(663): java.lang.NullPointerException 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at videothumb.videothumb.videothumb.onKeyDown(videothumb.java:138) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at android.view.KeyEvent.dispatch(KeyEvent.java:1037) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at android.app.Activity.dispatchKeyEvent(Activity.java:2068) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1735) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at android.os.Looper.loop(Looper.java:123) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at java.lang.reflect.Method.invoke(Method.java:521) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-04 18:57:52.906: ERROR/AndroidRuntime(663):  at dalvik.system.NativeStart.main(Native Method) 
10-04 18:57:52.926: WARN/ActivityManager(59): Force finishing activity videothumb.videothumb/.videothumb 
10-04 18:57:53.465: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{44f41b20 videothumb.videothumb/.videothumb} 
10-04 18:57:59.436: DEBUG/dalvikvm(196): GC_EXPLICIT freed 72 objects/2936 bytes in 99ms 
10-04 18:58:04.222: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{44f41b20 videothumb.videothumb/.videothumb} 
+0

請人幫我.... – balaji

回答

1

顯示alertdialog框和地方收益返回true super.onKeyDown(keyCode,event);

+0

公共布爾的onkeydown(INT的keyCode,KeyEvent的事件){ 如果((的keyCode == KeyEvent.KEYCODE_MENU)){ \t alertbox.show();; \t return true;} return super.onKeyDown(keyCode,event); } – balaji

+0

現在我試試這個代碼的崩潰我的應用程序.... – balaji

+0

發表您的logcat – Rocker