我正在使用与ICS操作栏相似的Sherlock Library操作栏,我被卡住的地方是这个

    @Override
public boolean onCreateOptionsMenu(Menu menu) {

    menu.add("Save")
    .setIcon(R.drawable.ic_compose).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    menu.add("Search")
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    SubMenu sub = menu.addSubMenu("Options");
    sub.add(0, SubMenu.NONE, 0, "First");
    sub.add(0,SubMenu.NONE, 1, "Second");
    sub.add(0, SubMenu.NONE, 2, "Three");
    sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    // HERE IS WHere I AM FACING PROBLEM IN
    getSupportMenuInflater().inflate(R.menu.share_action_provider, menu);
    // Set file with share history to the provider and set the share intent.
    actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
    ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
    actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME); //this is BRILLIANT WAY TO AVOID REPEATation
    actionProvider.setShareIntent(createShareIntent());

    return super.onCreateOptionsMenu(menu);
}

private Intent createShareIntent() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");

    //THIS TEXT IS WHAT I WANT TO OBTAIN DYNAMICALLY
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Something goes here");
    return shareIntent;
}

每当我尝试将actionProvider.setShareIntent(createShareIntent());放在监听器中或actionProvider.setShareIntent(createShareIntent());以外的任何地方时,我都会得到D/View(11753): onTouchEvent: viewFlags is DISABLED
我希望包括该共享Action提供程序,并希望输入我自己的文本,该文本是根据用户输入生成的。

欢迎任何输入。

添加此功能不起作用
    actionItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Log.e("THIS IS NEVER CALLED", "??");
            return true;
        }
    });

如何获得有关单击的“共享”按钮的更新

最佳答案

我用android.support.v7.app.ActionBarActivity派生工具遇到了类似的问题(尽管我认为这种方法适用于任何 Activity ),并且我覆盖了onPrepareOptionsMenu并获得了您想要的效果:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
    if (actionProvider != null) {
        actionProvider.setShareIntent(createShareIntent());
    }
    return super.onPrepareOptionsMenu(menu);
}

10-06 00:59