能运行 就是WebView控件显示不出网页来
代码:
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
<?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” >
<LinearLayout
android:orientation=”horizontal”
android:weightSum=”10″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”>
<EditText
android:layout_width=”match_parent”
android:layout_weight=”2″
android:layout_height=”wrap_content”
android:id=”@+id/etURL”/>
<Button
android:text=”转到”
android:id=”@+id/bGo”
android:layout_weight=”8″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”/>
</LinearLayout>
<LinearLayout
android:orientation=”horizontal”
android:weightSum=”8″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”>
<Button android:text=”返回”
android:id=”@+id/bBack”
android:layout_weight=”2″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”/>
<Button android:text=”向前”
android:id=”@+id/bForward”
android:layout_weight=”2″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”/>
<Button android:text=”刷新”
android:id=”@+id/bRefresh”
android:layout_weight=”2″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”/>
<Button android:text=”清除”
android:id=”@+id/bHistory”
android:layout_weight=”2″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”/>
</LinearLayout>
<WebView
android:id=”@+id/wvBrowser”
android:layout_width=”match_parent”
android:layout_height=”match_parent”/>”
</LinearLayout>
运行结果: