Android onKeyDown()在按下后退按钮时不执行

时间:2012-08-06 05:14:22

标签: android android-keypad

嗨,搜索谷歌后,我来这里询问帮助。当用户按下设备的后退按钮时,我使用以下方法返回。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {


        Log.i("===BACK BUTTON PRESSED===", "BACK BUTTON");

        return true;
    } else {
        Log.i("===ELSE BACK BUTTON PRESSED===", "ELSE BACK BUTTON");

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

但是logcat不显示任何日志消息。

W/KeyCharacterMap(517): No keyboard for id 0  

W/KeyCharacterMap(517): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 请帮我找到这个。

已编辑的问题:

这是我的全班。

public class Dreams_Reminder_detail_screen extends Activity {   

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        GroupDreams.group.back();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dreams_reminder_details_screen);        
}

}

6 个答案:

答案 0 :(得分:7)

您可以覆盖onBackPressed()功能并在那里实现您的逻辑。

请参阅此android博文"Back and other hard keys: three stories"以获得更多理解。

您可以在GroupDreams中覆盖onKeyDown函数,这可能会起作用

答案 1 :(得分:2)

试试此链接:Override back button to act like home button

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

答案 2 :(得分:1)

您可以通过以下方式追踪后退按钮事件: -

@Override
public void onBackPressed() {
    super.onBackPressed();

     //Do the Logics Here 
}

答案 3 :(得分:1)

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
//How to intercept the BACK key in an Activity is also one of the common questions we see //developers ask, so as of 2.0 we have a new little API to make this more simple and easier //to discover and get right:

@Override
public void onBackPressed() {
// do something on back.
return;
}

答案 4 :(得分:0)

尝试使用以下代码来帮助您

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) 
        {
             }
         }

答案 5 :(得分:0)

你在else {}中写了一个错误的超级方法。 像这样编辑你的代码

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {


            Log.i("===BACK BUTTON PRESSED===", "BACK BUTTON");

            return true;
        } else {
            Log.i("===ELSE BACK BUTTON PRESSED===", "ELSE BACK BUTTON");


        }
return super.onKeyDown(keyCode, event);

}