更新操作栏菜单

时间:2013-02-04 19:32:06

标签: android android-actionbar actionbarsherlock

我正在操作栏(sherlock操作栏)中隐藏我的刷新按钮。但是,我不知道如何在我的onPrepareOptionsMenu()函数中调用操作栏更新(InvalidateOptionsMenu()makeupdate())(我需要在函数内部进行此调用)。我一直在stackoverflow和谷歌搜索几个小时......测试了一切,什么也没得到......

我的makeupdate()功能:

public void makeupdate(String toast) {
    Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    MenuItem item = menu.findItem(R.id.refresh);
    item.setVisible(false);
}

编辑 - 我的代码

public class SiteActivity extends SherlockActivity {

    private WebView myWebView;
    final Activity MyActivity = this;
    com.actionbarsherlock.app.ActionBar actionbar;
    private Menu mainMenu;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 


        actionbar = getSupportActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionbar.setTitle("APP");
        actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);

        setContentView(R.layout.site);

        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");  
            myWebView.loadUrl("file:///android_asset/noconnection.html");

        myWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                MyActivity.setProgress(progress * 100);
            }
        });

        myWebView.setWebViewClient(new SiteActivityClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                myWebView.loadUrl("file:///android_asset/noconnection.html");
            }   
        });                 
    }

    public class WebAppInterface {
        Context mContext;

        WebAppInterface(Context c) {
            mContext = c;
        }
        @JavascriptInterface
        public void makeupdateshowToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
            mainMenu.findItem(R.id.refresh).setVisible(false);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getSupportMenuInflater().inflate(R.layout.actionbar, menu);
        mainMenu = menu;
        return true;
    }       
}

1 个答案:

答案 0 :(得分:2)

您的_ WebAppInterface_类已经有一个Context成员。使其成为SherlockActivity(如果更改名称,则更好)

public class WebAppInterface {
     SherlockActivity mActivity;

WebAppInterface(SherlockActivity c) {
    mActivity = c;
}
@JavascriptInterface
public void makeupdateshowToast(String toast) {
    Toast.makeText(mActivity, toast, Toast.LENGTH_SHORT).show();
    mainMenu.findItem(R.id.refresh).setVisible(false);
    mActivity.invalidateOptionsMenu();
}

}

并致电

mActivity.invalidateOptionsMenu()