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

查看:56
本文介绍了如何从带有按钮的片段中打开Web视图中的网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 https://stackoverflow.com/users/1116216/michele-la-ferla (米歇尔·拉·费拉)在这里:

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. 具有多个按钮的布局文件.
  2. 每个按钮在Web视图中加载不同的URL.

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

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.

片段事件:

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

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

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>

与此同时,我将查看按钮的官方Android文档 ActionListeners .

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

这篇关于如何从带有按钮的片段中打开Web视图中的网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆