browserhistory java,【新手问题】Android浏览器中WebView加载不出网页

能运行 就是WebView控件显示不出网页来

1430460961_336854.png

代码:

1.MainActivity.java

package com.example.simplebrowser;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.simplebrowser);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

2.SimpleBrowser.java

package com.example.simplebrowser;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.webkit.WebView;

import android.widget.Button;

import android.widget.EditText;

public class SimpleBrowser extends Activity implements OnClickListener{

private EditText url;

private WebView ourBrow;

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.simplebrowser);

ourBrow = (WebView)findViewById(R.id.wvBrowser);

ourBrow.loadUrl(“https://www.baidu.com”);

Button go = (Button)findViewById(R.id.bGo);

Button back = (Button)findViewById(R.id.bBack);

Button refresh = (Button)findViewById(R.id.bRefresh);

Button forward = (Button)findViewById(R.id.bForward);

Button clearHistory = (Button)findViewById(R.id.bHistory);

url = (EditText)findViewById(R.id.etURL);

go.setOnClickListener(this);

back.setOnClickListener(this);

refresh.setOnClickListener(this);

forward.setOnClickListener(this);

clearHistory.setOnClickListener(this);

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch(v.getId()){

case R.id.bGo:

String theWebsite = url.getText().toString();

ourBrow.loadUrl(theWebsite);

break;

case R.id.bBack:

if(ourBrow.canGoBack())

ourBrow.goBack();

break;

case R.id.bForward:

if(ourBrow.canGoForward())

ourBrow.goForward();

break;

case R.id.bRefresh:

ourBrow.reload();

case R.id.bHistory:

ourBrow.clearHistory();

break;

}

}

}

3.simplebrowser.xml

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:orientation=”vertical” >

android:orientation=”horizontal”

android:weightSum=”10″

android:layout_width=”match_parent”

android:layout_height=”wrap_content”>

android:layout_width=”match_parent”

android:layout_weight=”2″

android:layout_height=”wrap_content”

android:id=”@+id/etURL”/>

android:text=”转到”

android:id=”@+id/bGo”

android:layout_weight=”8″

android:layout_width=”match_parent”

android:layout_height=”wrap_content”/>

android:orientation=”horizontal”

android:weightSum=”8″

android:layout_width=”match_parent”

android:layout_height=”wrap_content”>

android:id=”@+id/bBack”

android:layout_weight=”2″

android:layout_width=”match_parent”

android:layout_height=”wrap_content”/>

android:id=”@+id/bForward”

android:layout_weight=”2″

android:layout_width=”match_parent”

android:layout_height=”wrap_content”/>

android:id=”@+id/bRefresh”

android:layout_weight=”2″

android:layout_width=”match_parent”

android:layout_height=”wrap_content”/>

android:id=”@+id/bHistory”

android:layout_weight=”2″

android:layout_width=”match_parent”

android:layout_height=”wrap_content”/>

android:id=”@+id/wvBrowser”

android:layout_width=”match_parent”

android:layout_height=”match_parent”/>”

运行结果:

1430460997_776635.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个例子: 1. 在res/layout文件夹创建一个名为fragment_webview.xml的布局文件,用于包含WebView。 ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout> ``` 2. 在Fragment类,声明WebView和ProgressBar,并在onCreateView方法将它们实例化并添加到布局。 ``` public class WebViewFragment extends Fragment { private WebView mWebView; private ProgressBar mProgressBar; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_webview, container, false); mWebView = view.findViewById(R.id.webview); mProgressBar = view.findViewById(R.id.progressBar); mWebView.setWebViewClient(new MyWebViewClient()); mWebView.loadUrl("https://www.example.com"); return view; } private class MyWebViewClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { mProgressBar.setVisibility(View.VISIBLE); } @Override public void onPageFinished(WebView view, String url) { mProgressBar.setVisibility(View.GONE); } } } ``` 3. 在WebViewClient重写onPageStarted和onPageFinished方法,以显示和隐藏ProgressBar。 这样,当WebView开始加载页面时,ProgressBar将显示在心,并在页面加载完成后自动消失。 注意:需要在AndroidManifest.xml文件添加Internet权限,以便WebView可以加载网页。 ``` <uses-permission android:name="android.permission.INTERNET" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值