我在这里阅读https://stackoverflow.com/users/1116216/michele-la-ferla(michele la ferla)的答案:how can i open a webpage in a webview from a fragment? 但是当你有更多的按钮用于" layout.xml"文件, 如何修改"片段类"文件? 这是我的第一个按钮的代码,错误在哪里?:
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;}}'
答案 0 :(得分:1)
如果我理解您的问题,您的方案由以下内容组成:
考虑到这些,我会在每个按钮上使用ActionListener实现它,并发送参数以在ActionEvent的webview中加载url。
片段事件:
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和ActionListeners的官方Android文档。