如何在AlertDialog框中使用KeyEvent?

时间:2011-10-04 12:56:37

标签: android alertdialog keyevent

我创建了一个警告对话框,它对我来说很好。现在我希望创建,如果我按下软键盘中的f2按钮我希望显示警报箱就是全部。我试过但它不起作用。请提供示例代码。

我的编码:

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我想显示对话框...

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}

1 个答案:

答案 0 :(得分:1)

在onKeyDown方法的if块中显示alertdialog框并返回true代替返回super.onKeyDown(keyCode,event);