本文介绍了尝试添加第二个链接的应用程序菜单的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要第二个链接添加到菜单。第一个一直很好。第二个叫 menu_about

 包com.test.apppackage;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.webkit.WebView;
进口android.webkit.WebViewClient;
进口android.widget.Toast;公共类MainActivity延伸活动{    私人的WebView mWebview;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        mWebview =新的WebView(本);        mWebview.getSettings()setJavaScriptEnabled(真)。 //启用JavaScript        最后活动活动=这一点;        mWebview.setWebViewClient(新WebViewClient(){
            公共无效onReceivedError(的WebView观点,诠释错误code,描述字符串,字符串failingUrl){
                Toast.makeText(活动介绍Toast.LENGTH_SHORT).show();
            }
        });        mWebview .loadUrl(https://www.google.com);
        的setContentView(mWebview);    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }
    公共布尔onOptionsItemSelected(菜单项项){
        INT ID = item.getItemId();
        开关(ID){
            案例com.homeovitality.productrecommender.R.id.menu_reset:
                mWebview .loadUrl(https://www.google.com/news);
                的setContentView(mWebview);
                打破;        }
        返回super.onOptionsItemSelected(项目);
    }    公共类MyWebViewClient扩展WebViewClient {        @覆盖
           公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
               view.loadUrl(URL);
               返回true;
           }
   }
    公共布尔onOptionsItemSelected(菜单项项){
        INT ID = item.getItemId();
        开关(ID){
            案例com.homeovitality.productrecommender.R.id.menu_about:
                mWebview .loadUrl(https://www.bbc.co.uk);
                的setContentView(mWebview);
                打破;        }
        返回super.onOptionsItemSelected(项目);
    }    公共类MyWebViewClient扩展WebViewClient {        @覆盖
           公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
               view.loadUrl(URL);
               返回true;
           }
   }
}


解决方案

您有2 onOptionsItemSelected

 公共布尔onOptionsItemSelected(菜单项项){//<  - 这
        INT ID = item.getItemId();
        开关(ID){
            案例com.homeovitality.productrecommender.R.id.menu_reset:
                mWebview .loadUrl(https://www.google.com/news);
                的setContentView(mWebview);
                打破;        }
        返回super.onOptionsItemSelected(项目);
    }    公共类MyWebViewClient扩展WebViewClient {        @覆盖
           公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){
               view.loadUrl(URL);
               返回true;
           }
   }
    公共布尔onOptionsItemSelected(菜单项项){//< - 这
        INT ID = item.getItemId();
        开关(ID){
            案例com.homeovitality.productrecommender.R.id.menu_about:
                mWebview .loadUrl(https://www.bbc.co.uk);
                的setContentView(mWebview);
                打破;        }
        返回super.onOptionsItemSelected(项目);
    }

删除其中之一,并切换ID是这样的:

 公共布尔onOptionsItemSelected(菜单项项){
        INT ID = item.getItemId();
        开关(ID){
            案例com.homeovitality.productrecommender.R.id.menu_reset:
                mWebview .loadUrl(https://www.google.com/news);
                的setContentView(mWebview);
                打破;
            案例com.homeovitality.productrecommender.R.id.menu_about:
                mWebview .loadUrl(https://www.bbc.co.uk);
                的setContentView(mWebview);
                打破;
        }
        返回super.onOptionsItemSelected(项目);
    }

i'm trying to add a second link to the menu. the first one has worked fine. the second is called menu_about

package com.test.apppackage;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

    private WebView mWebview ;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mWebview  = new WebView(this);

        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

        final Activity activity = this;

        mWebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });

        mWebview .loadUrl("https://www.google.com");
        setContentView(mWebview );



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_reset:
                mWebview .loadUrl("https://www.google.com/news");
                setContentView(mWebview );
                break;

        }
        return super.onOptionsItemSelected(item);
    }



    public class MyWebViewClient extends WebViewClient {

        @Override
           public boolean shouldOverrideUrlLoading(WebView view, String url) {
               view.loadUrl(url);
               return true;
           }
   }


    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_about:
                mWebview .loadUrl("https://www.bbc.co.uk");
                setContentView(mWebview );
                break;

        }
        return super.onOptionsItemSelected(item);
    }



    public class MyWebViewClient extends WebViewClient {

        @Override
           public boolean shouldOverrideUrlLoading(WebView view, String url) {
               view.loadUrl(url);
               return true;
           }
   }


}
解决方案

You have 2 onOptionsItemSelected:

public boolean onOptionsItemSelected(MenuItem item) { // <-- this
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_reset:
                mWebview .loadUrl("https://www.google.com/news");
                setContentView(mWebview );
                break;

        }
        return super.onOptionsItemSelected(item);
    }



    public class MyWebViewClient extends WebViewClient {

        @Override
           public boolean shouldOverrideUrlLoading(WebView view, String url) {
               view.loadUrl(url);
               return true;
           }
   }


    public boolean onOptionsItemSelected(MenuItem item) { // <-- and this
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_about:
                mWebview .loadUrl("https://www.bbc.co.uk");
                setContentView(mWebview );
                break;

        }
        return super.onOptionsItemSelected(item);
    }

Delete one of them and switch the id like this:

public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_reset:
                mWebview .loadUrl("https://www.google.com/news");
                setContentView(mWebview );
                break;
            case com.homeovitality.productrecommender.R.id.menu_about:
                mWebview .loadUrl("https://www.bbc.co.uk");
                setContentView(mWebview );
                break;


        }
        return super.onOptionsItemSelected(item);
    }

这篇关于尝试添加第二个链接的应用程序菜单的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 15:06