gpt4 book ai didi

android - 我如何从带有按钮的 fragment 中打开 webview 中的网页?

转载 作者:行者123 更新时间:2023-11-30 02:35:56 25 4
gpt4 key购买 nike

我看了https://stackoverflow.com/users/1116216/michele-la-ferla的答案(米歇尔·拉费拉)在这里:how can i open a webpage in a webview from a fragment?但是当你在“layout.xml”文件中有更多按钮以获得更多 URL 时,如何修改你的“fragment 类”文件? 这是我的第一个按钮的代码,错误在哪里?:

   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 来实现它,并发送参数以在 ActionEvent 的 webview 中加载 url。

fragment 事件:

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 文档。和 ActionListeners也是。

关于android - 我如何从带有按钮的 fragment 中打开 webview 中的网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26557016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com