开发者

Android overriding onKeyDown within Dialog

开发者 https://www.devze.com 2023-03-17 05:27 出处:网络
I have an activity which opens up a Dialog as soon as its openeed: /** Called when the activity is first created. */

I have an activity which opens up a Dialog as soon as its openeed:

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

    mPath = new File(getIntent().getExtras().getString("START_PATH"));
    FTYPE = getIntent().getExtras().开发者_开发知识库getString("FILTER");

    showDialog(DIALOG_LOAD_FILE);        

}

I'm trying to make it so when the user clicks the back button (while still within the Dialog), the entire activity is exited. I have the following:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {

            Intent resultIntent = new Intent();
            resultIntent.putExtra("FILE_PATH", mPath.toString());
            setResult(Activity.RESULT_OK, resultIntent);
            finish();           
    }

   return true;
}

This works great if the Dialog is no longer there, but doesn't get called if the Dialog is still active. Thoughts?


try this

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                this);
        // set prompts.xml to alertdialog builder
        alertDialogBuilder.setView(promptsView);
        alertDialogBuilder
        .setCancelable(true);

        // create alert dialog
        final AlertDialog dialogDiscount = alertDialogBuilder.create();

        // show it
        dialogDiscount.show();

        OnKeyListener keylistener = new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent KEvent) {
                int keyaction = KEvent.getAction();

                if(keyaction == KeyEvent.ACTION_DOWN)
                {
                    int keycode = KEvent.getKeyCode();
                    int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState() );
                    char character = (char) keyunicode;
                    if(keycode==KeyCode.Enter){


                    }
                }
                return false;
            }
        };
        dialogDiscount.setOnKeyListener(keylistener );


Have you tried overriding onKeyDown() in the Dialog class when you instantiate the Dialog? I haven't tested it, but try something like

@Override
protected void onCreateDialog(int id) {
    Dialog dialog = null;
    switch(id) {
    case DIALOG_LOAD_FILE:
        dialog = new AlertDialog(mContext) {
            @Override
            public boolean onKeyDown(int keyCode, KeyEvent event) {
                if(keyCode == KeyEvent.KEYCODE_BACK) {
                    dismiss();
                    finish();
                    return true;
                }

                return super.onKeyDown(keyCode, event);
            }
        };   
        break;
    }
    return dialog;
}


you must create an object of DialogInterface.OnKeyListener and in that you can override onKey(DialogInterface dialog, int keyCode, KeyEvent event). this method capture key event inside a dialog. example:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
//do something to your dialog like: dialogBuilder.setView(view);
DialogKeyListener dkl = new DialogKeyListener();        
dialogBuilder.setOnKeyListener(dkl);
AlertDialog dialog = dialogBuilder.create();
dialog.show();

and listener class:

private class DialogKeyListener implements android.content.DialogInterface.OnKeyListener
{
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) 
{
    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
        dialog.dismiss();
        yourClassActivity.finish(); // or something like that
        return false;
    }
    return true
}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消