6

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

@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);        
}

}

4

6 回答 6

8

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

请参考这篇android博文“返回和其他硬键:三个故事”以获得更多理解。

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

于 2012-08-06T05:22:07.497 回答
2

试试这个链接:覆盖后退按钮以充当主页按钮

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
于 2012-08-06T05:40:02.963 回答
1

您可以通过这种方式跟踪后退按钮事件:-

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

     //Do the Logics Here 
}
于 2012-08-06T05:17:44.810 回答
1

试试下面的代码,它会帮助你

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) 
        {
             }
         }
于 2012-08-06T05:25:35.087 回答
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;
}
于 2014-04-03T10:29:14.217 回答
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);

}
于 2012-08-06T05:32:55.557 回答