如何从带有按钮的片段中打开WebView中的网页?[英] how can i open a webpage in a webview from a fragment with buttons?

本文是小编为大家收集整理的关于如何从带有按钮的片段中打开WebView中的网页?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我阅读了 https://stackoverflow.com/users/1116216/1116216/michele-la-la-la-ferla (Michele la Ferla)在这里:我如何从片段中打开网页? 但是,当您在" layout.xml"文件中有更多url的按钮时, 如何修改您的"片段类"文件? 这是我的第一个按钮的代码,错误在哪里?:

   private Dialog WebDialog1;
    private WebView URL1;
    private Button button0;

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

        final View rootView = inflater.inflate(R.layout.fragmentsite, container, false);

        button0 = (Button) rootView.findViewById(R.id.button0);
        button0.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                WebDialog1 = new Dialog(getActivity());
                WebDialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
                WebDialog1.setContentView(R.layout.sitef);
                WebDialog1.setCancelable(true);

                URL1 = (WebView) rootView.findViewById(R.id.webview);
                URL1.setWebViewClient(new WebViewClient());
                URL1.setScrollbarFadingEnabled(true);
                URL1.setHorizontalScrollBarEnabled(false);
                URL1.getSettings().setJavaScriptEnabled(true);
                URL1.loadUrl("http://www.dhfhfhfh.com/Home.php");

                WebDialog1.show();
            }



        });return rootView;}}'

推荐答案

如果我很好地了解了您的问题,则您的场景由以下内容组成:

  1. 一个包含多个按钮的布局文件.
  2. 每个按钮在WebView中加载不同的URL.

考虑到这些,我将使用每个按钮上的ActionListener实现它,并发送参数以将URL加载到ActionEvent中的WebView中.

片段事件:

public class EventFragment extends Fragment {

private Dialog WebDialog1, WebDialog2;
private WebView URL1, URL2;
private Button btnURL1, btnURL2;

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

    View rootView = inflater.inflate(R.layout.main_layout, container, false);

    btnURL1 = (Button) rootView.findViewById(R.id.btnURL1);
    btnURL1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            WebDialog1 = new Dialog(getActivity());
            WebDialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
            WebDialog1.setContentView(R.layout.web_layout);
            WebDialog1.setCancelable(true);                  

            URL1 = (WebView) WebDialog.findViewById(R.id.url1);  
            URL1.setWebViewClient(new WebViewClient());
            URL1.setScrollbarFadingEnabled(true);  
            URL1.setHorizontalScrollBarEnabled(false);  
            URL1.getSettings().setJavaScriptEnabled(true);
            URL1.getSettings().setUserAgentString("First Webview");  
            URL1.loadUrl("//the first url goes here");

            WebDialog1.show();
        }

    btnURL2 = (Button) rootView.findViewById(R.id.btnURL2);
    btnURL2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            WebDialog2 = new Dialog(getActivity());
            WebDialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
            WebDialog2.setContentView(R.layout.web_layout);
            WebDialog2.setCancelable(true);                  

            URL2 = (WebView) WebDialog.findViewById(R.id.url2);  
            URL2.setWebViewClient(new WebViewClient());
            URL2.setScrollbarFadingEnabled(true);  
            URL2.setHorizontalScrollBarEnabled(false);  
            URL2.getSettings().setJavaScriptEnabled(true);
            URL2.getSettings().setUserAgentString("Second Webview");  
            URL2.loadUrl("//the second url goes here");

            WebDialog2.show();
        }

    });
    return rootView;
}

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btnURL1"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"

    <Button
        android:id="@+id/btnURL2"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"

</RelativeLayout>

web_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/rl_relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dip"
        android:layout_weight="0.82" >

        <TextView
            android:id="@+id/Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:text="Buy your Tickets:" />

        <WebView
            android:id="@+id/ticketline"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/Title"
            android:layout_marginTop="5dip" />
    </RelativeLayout>

</LinearLayout>

在此时,我会查看 buttons的官方Android文档 and actionListeners 也是.

.

本文地址:https://www.itbaoku.cn/post/2091051.html

问题描述

I read the answer of https://stackoverflow.com/users/1116216/michele-la-ferla (michele la ferla) here: how can i open a webpage in a webview from a fragment? but when you have more buttons for more URL in "layout.xml" file, how modify you the "fragment class" file? this is my code for the first button, where is the error?:

   private Dialog WebDialog1;
    private WebView URL1;
    private Button button0;

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

        final View rootView = inflater.inflate(R.layout.fragmentsite, container, false);

        button0 = (Button) rootView.findViewById(R.id.button0);
        button0.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                WebDialog1 = new Dialog(getActivity());
                WebDialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
                WebDialog1.setContentView(R.layout.sitef);
                WebDialog1.setCancelable(true);

                URL1 = (WebView) rootView.findViewById(R.id.webview);
                URL1.setWebViewClient(new WebViewClient());
                URL1.setScrollbarFadingEnabled(true);
                URL1.setHorizontalScrollBarEnabled(false);
                URL1.getSettings().setJavaScriptEnabled(true);
                URL1.loadUrl("http://www.dhfhfhfh.com/Home.php");

                WebDialog1.show();
            }



        });return rootView;}}'

推荐答案

If I understood your question well, your scenario is composed of the following:

  1. A layout file with more than one button.
  2. Each button loads a different url in a webview.

Taking these into consideration, I would implement it using an ActionListener on each button, and sending the parameters to load the url in a webview in the ActionEvent.

Fragment event:

public class EventFragment extends Fragment {

private Dialog WebDialog1, WebDialog2;
private WebView URL1, URL2;
private Button btnURL1, btnURL2;

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

    View rootView = inflater.inflate(R.layout.main_layout, container, false);

    btnURL1 = (Button) rootView.findViewById(R.id.btnURL1);
    btnURL1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            WebDialog1 = new Dialog(getActivity());
            WebDialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
            WebDialog1.setContentView(R.layout.web_layout);
            WebDialog1.setCancelable(true);                  

            URL1 = (WebView) WebDialog.findViewById(R.id.url1);  
            URL1.setWebViewClient(new WebViewClient());
            URL1.setScrollbarFadingEnabled(true);  
            URL1.setHorizontalScrollBarEnabled(false);  
            URL1.getSettings().setJavaScriptEnabled(true);
            URL1.getSettings().setUserAgentString("First Webview");  
            URL1.loadUrl("//the first url goes here");

            WebDialog1.show();
        }

    btnURL2 = (Button) rootView.findViewById(R.id.btnURL2);
    btnURL2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            WebDialog2 = new Dialog(getActivity());
            WebDialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
            WebDialog2.setContentView(R.layout.web_layout);
            WebDialog2.setCancelable(true);                  

            URL2 = (WebView) WebDialog.findViewById(R.id.url2);  
            URL2.setWebViewClient(new WebViewClient());
            URL2.setScrollbarFadingEnabled(true);  
            URL2.setHorizontalScrollBarEnabled(false);  
            URL2.getSettings().setJavaScriptEnabled(true);
            URL2.getSettings().setUserAgentString("Second Webview");  
            URL2.loadUrl("//the second url goes here");

            WebDialog2.show();
        }

    });
    return rootView;
}

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btnURL1"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"

    <Button
        android:id="@+id/btnURL2"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"

</RelativeLayout>

web_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/rl_relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dip"
        android:layout_weight="0.82" >

        <TextView
            android:id="@+id/Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:text="Buy your Tickets:" />

        <WebView
            android:id="@+id/ticketline"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/Title"
            android:layout_marginTop="5dip" />
    </RelativeLayout>

</LinearLayout>

While at it, I would take a look at the official Android documentation for buttons and ActionListeners too.