1

我想显示我的活动的上下文菜单,同时保持显示软键盘。这是我为我的主要活动创建菜单的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    boolean result = super.onCreateOptionsMenu(menu);

    MenuItem m = menu.add(0, LOGIN_MENU_ITEM, 0, "Login");
    m.setIcon(android.R.drawable.ic_menu_preferences); 
    m = menu.add(0, CONFIGURE_MENU_ITEM, 0, R.string.menu_settings);
    m.setIcon(android.R.drawable.ic_menu_preferences);
    m = menu.add(0, EXIT_MENU_ITEM, 0, R.string.menu_exit);
    m.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
    m = menu.add(0, ABOUT_MENU_ITEM, 0, R.string.menu_about);
    m.setIcon(android.R.drawable.ic_menu_info_details);

    return result;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    boolean result = super.onOptionsItemSelected(item);
    Intent intent = null;

    switch (item.getItemId()) {
    case ABOUT_MENU_ITEM:
        if (m_AlertDlg != null) 
        {
            m_AlertDlg.cancel();
        }

        final SpannableString s = new SpannableString(getString(R.string.about).replace("\\n","\n").replace("${VERSION}", getVersion(this)));
        Linkify.addLinks(s, Linkify.WEB_URLS);

        m_AlertDlg = new AlertDialog.Builder(this)
        //          .setMessage(getString(R.string.about).replace("\\n","\n").replace("${VERSION}", getVersion(this)))
        .setMessage(s)
        .setTitle(getString(R.string.menu_about))
        .setIcon(R.drawable.icon22_click)
        .setCancelable(true)
        .show();

        ((TextView)m_AlertDlg.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

        break;

    case EXIT_MENU_ITEM: 
        on(this,false);
        //          final Boolean exit_rem = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Settings.PREF_REMEMBER, false);
        //          if (!exit_rem){
        //              Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
        //              edit.putString(Settings.PREF_USERNAME, "");
        //              edit.putString(Settings.PREF_PASSWORD, "");
        //              edit.commit();
        //          }
        Receiver.pos(true);
        Receiver.engine(this).halt();
        Receiver.mSipdroidEngine = null;
        Receiver.reRegister(0);
        stopService(new Intent(this,RegisterService.class));
        finish();
        break;

    case CONFIGURE_MENU_ITEM: 
        try {
            intent = new Intent(this, org.sparksoftsip.sipua.ui.Settings.class);
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
        }
        break;

    case LOGIN_MENU_ITEM: 
        LoginScreen();
        break;
    }

    return result;
}

在我的主要活动中,我构建了一个自定义 AutoCompleteTextView,其中它接收焦点在开始。我在这里听按键,并计划通过听菜单按钮打开上下文菜单。但是,我当前的代码没有任何反应:

public class InstantAutoCompleteTextView extends AutoCompleteTextView {
public InstantAutoCompleteTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
    super.onWindowFocusChanged(hasWindowFocus);
}
private Activity getActivity() {
    Context context = getContext();
    if (context instanceof Activity) {
        return (Activity) context;
    } else {
        return null;
    }
}
@Override
public boolean enoughToFilter() {
    return true;
}


@Override
public boolean dispatchKeyEventPreIme(KeyEvent event)
{
    Activity activity = getActivity();
    if (KeyEvent.KEYCODE_BACK == event.getKeyCode() && event.getAction () == KeyEvent.ACTION_DOWN)
    {
        Log.v("", "Back Pressed");
        activity.onBackPressed();

        //Want to call this method which will append text
        //init();
    }

    if (KeyEvent.KEYCODE_MENU == event.getKeyCode() && event.getAction () == KeyEvent.ACTION_DOWN)
    {
        Log.e("hello", event.toString());
        activity.openContextMenu(activity.getWindow().getDecorView().findViewById(android.R.id.content));
    }
    return true;
    //return super.dispatchKeyEventPreIme(event);
}
}

我试过使用

openContextMenu(activity.getWindow().getDecorView().findViewById(android.R.id.content))

但是,什么都没有发生。我也试过(activity.findViewById(android.R.id.content))and (activity.findViewById(android.R.id.content).getViewRoot()),但没有任何结果。我该如何补救?

4

1 回答 1

1

看来我犯了一个愚蠢的错误。我没有使用上下文菜单,而是使用选项菜单。所以,我的按键代码应该是

openOptionsMenu()

而不是使用

openContextMenu(View)
于 2012-09-27T03:48:20.507 回答