添加标签到webview以在Android应用中打开不同的网站

时间:2017-02-15 23:39:25

标签: java android webview android-tabs

我的应用是全屏活动。它是一个webview应用程序,我想添加三个选项卡,使每个选项卡打开一个不同的站点。

这是我的主要活动java

package com.wEgyptpost.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.TextView;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;




import java.util.Random;

import com.google.android.gms.ads.AdRequest;

import com.google.android.gms.ads.AdView;

import com.wEgyptpost.app.R;


public class MainActivity extends Activity {

    WebView view;
    SwipeRefreshLayout mySwipeRefreshLayout;
    ProgressBar progressBar;
    private WebView mWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AdView adView = (AdView) findViewById(R.id.admob_id);
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();

        adView.loadAd(adRequest);



        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        progressBar= (ProgressBar)findViewById(R.id.progressBar);
        final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);
        final WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
        swipeRefreshLayout.setColorSchemeResources(R.color.refresh,R.color.refresh1,R.color.refresh2);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mWebView.reload();
                swipeRefreshLayout.setRefreshing(true);

                (new Handler()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mWebView.stopLoading();
                        swipeRefreshLayout.setRefreshing(false);





                    }
                },20000);
            }
        });
        // Force links and redirects to open in the WebView instead of in a browser
        //mWebView.setWebViewClient(new WebViewClient());

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        // Use remote resource
         mWebView.loadUrl("www.google.com");

        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(final WebView view, String url) {

                progressBar.setVisibility(View.GONE);
                swipeRefreshLayout.setRefreshing(false);

            }
        } );

        // Use local resource
        //mWebView.loadUrl("file:android_asset/web/google.html");


    }



    // Prevent the back-button from closing the app
    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



}

这是活动xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">


    <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
        <WebView
            android:id="@+id/activity_main_webview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />


        <ScrollView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">

   </ScrollView>


</android.support.v4.widget.SwipeRefreshLayout >

    <com.google.android.gms.ads.AdView android:id="@+id/admob_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-1851250777225639/8300259415"
        android:layout_gravity="center_horizontal|bottom" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_gravity="center" />


</FrameLayout>

0 个答案:

没有答案