android - 如何在单个 webview 中使用多个 url

标签 android android-webview

我是 webView 的新手,在这里,当我单击文本 1 并在 web View 中打开时,我必须单击文本 2,在同一 web View 中打开任何一个,请如何在每个 textview 中放置两个 onclick 事件字符串来自服务器 我尝试了这种方法,但没有任何用处,请帮助我,我搜索谷歌,但他们没有用

下面是我的代码

Activity .java

 //webview onclick and get bundle
    webviewurl=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl();

    webviewurl2=NewsMainFregmant_List.listData.get(pos).getNewsSourceUrl2();

    news_site_link_one=(TextView)findViewById(R.id.news_SourceLink_text_one_t_webview);
    news_site_like_two=(TextView)findViewById(R.id.news_SourceLink_text_two_t_webview);

  news_site_link_one.setOnClickListener(new View.OnClickListener() {
      @Override
       public void onClick(View v) {
        Intent webviewintent = new Intent(getApplicationContext(), News_WebView.class);
       webviewintent.putExtra("webviewurl", webviewurl);
         startActivity(webviewintent);

    }
   });


    news_site_like_two.setOnClickListener(new View.OnClickListener() {
     @Override
      public void onClick(View v) {
        Intent webviewintent = new Intent(getApplicationContext(), News_WebView.class);
          webviewintent.putExtra("webviewurl2", webviewurl2);
      startActivity(webviewintent);


     }
   });

这是我的 WebView 代码

 String SourceURL;
WebView webview;
final Activity activity = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news__web_view);


    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    setContentView(R.layout.activity_news__web_view);

    Intent intent =this.getIntent();

    if(intent!=null)


        SourceURL =  intent.getStringExtra("webviewurl");
    if(SourceURL.equals("webviewurl")) {

        webview = (WebView) findViewById(R.id.webView);
        webview.getSettings().setJavaScriptEnabled(true);
    }

    if(SourceURL.equals("webviewurl2")) {

        webview = (WebView) findViewById(R.id.webView);
        webview.getSettings().setJavaScriptEnabled(true);
    }





    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }
    });

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            // Handle the error
        }

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

    webview.loadUrl(SourceURL);
}

最佳答案

使用 containsKey

Bundle data =getIntent().getExtras();

if(data.containsKey("webviewurl"))
{
    SourceURL =data.getString("webviewurl");
}
else if(data.containsKey("webviewurl2"))
{
                SourceURL =data.getString("webviewurl2");
    }

关于android - 如何在单个 webview 中使用多个 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36003530/

相关文章:

android - ftello 函数未声明

java - 编译 Ubuntu Touch 无规则使目标为 "APPS/../src/R.stamp"

android - 在实际的智能手机上安装 apk

java - 如何在 Android WebView 中访问 WhatsAppWeb?

c# - 如何在打开指定 URL 时关闭 WebViewClient

javascript - PhoneGap javascript 引用问题

android - HTML 5 音频播放器和 Android/iOS

android - WebViewClient 不调用 shouldOverrideUrlLoading

javascript - 如何使用 loadDataWithBaseURL() 方法从 SDCARD 加载 JS 和 CSS

java - socket.io-client java 未连接到服务器