我想使用Intent从MainActivity调用WebView Activity,我在string-array.xml中存储了5个url

时间:2017-03-06 13:10:58

标签: java android android-intent webview

我想使用WebViewMainActivity致电Intent来自string-array.xml,并在List item

中存储5个网址

我想要的内容:点击MainActivity中的WebView,它应该会打开[position]个活动并使用list-item它应该打开与之关联的确切网址string-array.xml: <string-array name="bookmark_urls"> <item>http://www.google.com</item> <item>http://www.android.com/</item> <item>http://www.toi.com/</item> <item>http://www.facebook.com/</item> <item>http://www.makemytrip.com/</item> <item>http://www.yatra.com/</item> </string-array> 代码说明:

MainActivity.java

String[] urls = getResources().getStringArray(R.array.bookmark_urls); Intent i = new Intent(MainActivity.this, Web_view.class); i.putExtra("urls", urls[position]); startActivity(i); 我称之为意图:

String urls

那么,我如何正确地将Webview拨打到下一个mainWebView.loadUrl("http://www.google.com");

我想要这样的事情:  for

5 个答案:

答案 0 :(得分:0)

在你的webActivity中执行此操作

Intent i = getIntent();
String url = i.getStringExtra("urls");

 WebView mywebview = (WebView) findViewById(R.id.webView1);  
 mywebview.loadUrl(url);  

答案 1 :(得分:0)

请尝试使用我的技巧,它肯定会起作用

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      WebView mainWebView = (WebView) findViewById(R.id.mainWebView);// pass here your webview id

      WebSettings settings = mainWebView.getSettings();
      settings.setJavaScriptEnabled(true);
      settings.setDomStorageEnabled(true);

      String urlStr = getIntent().getStringExtra("urls");
      mainWebView.loadUrl(urlStr);
}

答案 2 :(得分:0)

WebView  ww = (WebView)findViewById(R.id.ww);
WebSettings webSettings = ww.getSettings();     
 webSettings.setJavaScriptEnabled(true);
 String WebURL=getIntent().getStringExtra("urls")
 ww.loadUrl(WebURL);

答案 3 :(得分:0)

在活动onCreate的{​​{1}}中获取并加载网址:

Web_view

答案 4 :(得分:0)

## Fragment with base Fragment ##

在Fragment的构造函数中创建传递URL,如下所示

mFragment = new Fragment1()。newInstance(“”+ position,“url”);

 - Fragment1.class


package com.mydemo.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.myadhaar.R;
import com.mydemo.Utils.Constants;
import com.mydemo.interfaces.OnFragmentInteractionListener;


public class Fragment1 extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String Fragmentposition;
    private String param2,URL;
    View rootView;
    private ProgressBar probressbar;
    WebView mWebView;

    private OnFragmentInteractionListener mListener;

    public Fragment1()
    {

    }


    public static Fragment1 newInstance(String Fragmentposition, String param2)
    {
        Fragment1 fragment = new Fragment1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, Fragmentposition);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

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

        if (getArguments() != null)
        {
            Fragmentposition = getArguments().getString(ARG_PARAM1);
            param2 = getArguments().getString(ARG_PARAM2);
            URL = param2;// Constants.Url[Integer.parseInt(Fragmentposition)];
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_adrno, container, false);
        initViews();
        setToViews();
        clickToViews();

        return rootView;
    }

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

        setwebView(mWebView,probressbar,URL);
    }

    @Override
    public void initViews() {
        super.initViews();

        mWebView = (WebView) rootView.findViewById(R.id.webview);
        probressbar = (ProgressBar) rootView.findViewById(R.id.progressBar);

    }

    @Override
    public void setToViews() {
        super.setToViews();
    }

    @Override
    public void clickToViews() {
        super.clickToViews();
    }

    @Override
    public void setwebView(WebView webView,ProgressBar probressbar, String URL) {
        super.setwebView(webView,probressbar, URL);

    }


    public void onButtonPressed(Uri uri)
    {
        if (mListener != null)
        {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if (context instanceof OnFragmentInteractionListener)
        {
            mListener = (OnFragmentInteractionListener) context;
        } else
        {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach()
    {
        super.onDetach();
        mListener = null;
    }

}

 1. BaseFragment.class


package com.mydemo.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebViewFragment;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.myadhaar.R;
import com.mydemo.interfaces.FragmentViews;


public class BaseFragment extends Fragment implements FragmentViews{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    FrameLayout frame_container;


    public BaseFragment()
    {

    }


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

        if (getArguments() != null)
        {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

   /* @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_base, container, false);
        frame_container = (FrameLayout) mRelativeLayout.findViewById(R.id.frame_container);
        super.onCreateView(inflater,  mRelativeLayout, savedInstanceState);

        return mRelativeLayout;
    }*/


    public void onButtonPressed(Uri uri)
    {

    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);

    }

    @Override
    public void onDetach()
    {
        super.onDetach();

    }

    @Override
    public void initViews() {

    }

    @Override
    public void setToViews() {

    }

    @Override
    public void clickToViews() {

    }

    @Override
    public void setwebView(WebView mWebView, final ProgressBar probressbar, String URL) {

        probressbar.setProgress(0);
        probressbar.setMax(100);
        WebSettings mWebSettings = mWebView.getSettings();
        mWebView.setWebViewClient(new WebViewClient());
        mWebSettings.setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
        mWebSettings.setBuiltInZoomControls(true);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setUseWideViewPort(true);
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setSaveFormData(true);
        mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        mWebSettings.setBuiltInZoomControls(true);
        mWebSettings.setAppCachePath( getActivity().getApplicationContext().getCacheDir().getAbsolutePath() );
        mWebSettings.setAllowFileAccess( true );
        mWebSettings.setAppCacheEnabled( true );
        mWebSettings.setJavaScriptEnabled( true );
        mWebSettings.setCacheMode( WebSettings.LOAD_DEFAULT );

        mWebView.loadUrl(/*URL*/"");
        mWebView.setWebViewClient(new MyWebViewClient()
        {
            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                super.onPageStarted(view, url, favicon);
                probressbar.setVisibility(View.VISIBLE);
            }

            public void onPageFinished(WebView view, String url)
            {
                super.onPageFinished(view, url);
                probressbar.setVisibility(View.GONE);
            }

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

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
                handler.cancel();
            }
        });

        if (Build.VERSION.SDK_INT > 11)
        {
            mWebView.getSettings().setDisplayZoomControls(false);
        }

    }



}

 3. Inteface that should implent on BaseFragment.class


public interface FragmentViews {

    public abstract void initViews();
    public abstract void setToViews();
    public abstract void clickToViews();
    public abstract void setwebView(WebView webView, ProgressBar probressbar,String URL);

}