这一次我对网页的浏览添加前进后退,退出,返回主页的功能。
上代码,
public class MainActivity extends Activity implements View.OnClickListener {
private EditText et_url;
private Button btn_visit;
private WebView web;
private Button btn_home,btn_left,btn_right,btn_exit;
private boolean isExit;
private String homeStr = "http://www.baidu.com";
//-------------------------------------------
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
init();
}
//-------------------------------------------
/**
* 基本数据初始化
*/
private void init(){
isExit = false;
et_url = (EditText)findViewById(R.id.et_url);
et_url.setText(homeStr);
btn_visit = (Button)findViewById(R.id.btn_visit);
web = (WebView)findViewById(R.id.web);
WebSettings ws = web.getSettings();
//是否允许脚本支持
ws.setJavaScriptEnabled(true);
web.setWebViewClient(wvc);
btn_home = (Button)findViewById(R.id.btn_home);
btn_left = (Button)findViewById(R.id.btn_left);
btn_right = (Button)findViewById(R.id.btn_right);
btn_exit = (Button)findViewById(R.id.btn_exit);
btn_visit.setOnClickListener(this);
btn_home.setOnClickListener(this);
btn_left.setOnClickListener(this);
btn_right.setOnClickListener(this);
btn_exit.setOnClickListener(this);
conn(et_url.getText().toString());
}
//-------------------------------------------
WebViewClient wvc = new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
et_url.setText(url);
return true;
};
};
//-------------------------------------------
/**
* 访问url
* @param urlStr
*/
private void conn(String urlStr){
String url = "";
if(urlStr.contains("http://")){
url = urlStr;
}else{
url = "http://"+urlStr;
}
web.loadUrl(url);
}
//-------------------------------------------
/**
* 后退
*/
private void goBack(){
if(web.canGoBack()){
web.goBack();
}else{
Toast.makeText(this, "已经是第一页", Toast.LENGTH_SHORT).show();
}
}
//-------------------------------------------
/**
* 前进
*/
private void goForward(){
if(web.canGoForward()){
web.goForward();
}else{
Toast.makeText(this, "已经是最后一页",Toast.LENGTH_SHORT).show();
}
}
//-------------------------------------------
/**
* 退出
*/
private void exit(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示")
.setMessage("确定退出?")
.setPositiveButton("确定", dialogListener)
.setNegativeButton("取消", dialogListener)
.create()
.show();
}
DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
finish();
break;
default:
dialog.cancel();
break;
}
}
};
//-------------------------------------------
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK||web.canGoBack()){
web.goBack();
if(!web.canGoBack()){
if(isExit){
return super.onKeyDown(keyCode, event);
}
isExit = true;
Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
}else{
isExit = false;
}
}
return true;
}
//-------------------------------------------
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_visit:
conn(et_url.getText().toString());
break;
case R.id.btn_home:
conn(homeStr);
break;
case R.id.btn_left:
goBack();
break;
case R.id.btn_right:
goForward();
break;
case R.id.btn_exit:
exit();
break;
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<EditText
android:id="@+id/et_url"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textUri"
android:singleLine="true"
android:layout_margin="10dp"/>
<Button
android:id="@+id/btn_visit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="visit"
android:textSize="15dp" />
</LinearLayout>
<WebView
android:id="@+id/web"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/ll_btn"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="home"/>
<Button
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="left"/>
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="right"/>
<Button
android:id="@+id/btn_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="exit"/>
</LinearLayout>
</LinearLayout>
<uses-permission android:name="android.permission.INTERNET"/>
因为这其实是从上一篇的基础上加了一些UI设计罢了,没啥知识点,下期我做网页手指触摸放大放小,请期待吧
再见
原文地址:http://blog.51cto.com/13591594/2067133
时间: 2024-10-11 22:19:23