2

在我下面的片段中

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    root = (ViewGroup) inflater.inflate(R.layout.layout1, null);
    setHasOptionsMenu(true);
    activity = getActivity();

菜单功能一直有效,直到我添加了一个 onKeyListener。在我添加 onKeyListener 后,菜单不起作用!

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    root = (ViewGroup) inflater.inflate(R.layout.layout1, null);
    setHasOptionsMenu(true);
    activity = getActivity();
    root.setFocusableInTouchMode(true);
    root.requestFocus();
    root.setOnKeyListener(onKey);
 .
 .
 .
 .
 OnKeyListener onKey = new OnKeyListener() {

    public boolean onKey(View arg0, int keyCode, KeyEvent arg2) {

    }
}

在片段中工作的解决方案是什么?

4

1 回答 1

0

您是否测试过在您的 onCreate 中调用 setHasOptionsMenu ?

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}
于 2014-10-21T00:36:21.297 回答