Android--在程序里浏览网页/Webview的使用

在说明本实例之前,首先给出程序的运行截图:


在程序中需要一个Webview控件用于显示网页,下方的两个按钮一个是回到前一页,一个是去往下一页,保存在混村中。

下面给出程序的实现代码:

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"

  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >

  <!-- 建立一個EditText -->
  <!-- 建立一個ImageButton -->
  <!-- 建立一個WebView -->

  <EditText
      android:id="@+id/myEditText1"
      android:layout_width="267px"
      android:layout_height="60px"
      android:layout_x="13dp"
      android:layout_y="38dp"
      android:ems="10"
      android:textSize="18sp" />

  <ImageButton
      android:id="@+id/myImageButton1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_x="206dp"
      android:layout_y="38dp"
      android:background="@drawable/white"
      android:src="@drawable/go" />

  <WebView
      android:id="@+id/myWebView1"
      android:layout_width="match_parent"
      android:layout_height="340dp"
      android:layout_x="0dp"
      android:layout_y="79dp"
      android:background="@drawable/black"
      android:focusable="false" />

  <Button
      android:id="@+id/buttonback"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_x="38dp"
      android:layout_y="426dp"
      android:text="Back   " />

  <Button
      android:id="@+id/buttonfoward"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_x="179dp"
      android:layout_y="426dp"
      android:text="Forward" />
 
</AbsoluteLayout>

2.主程序文件

package irdc.ex08_02;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class EX08_02 extends Activity 
{
  private String TAG = "HIPPO_DEBUG";
  private ImageButton mImageButton1;
  private EditText mEditText1;
  private WebView mWebView1;  
  private Button button,button2;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   
    mImageButton1 = (ImageButton)findViewById(R.id.myImageButton1);
    mEditText1 = (EditText)findViewById(R.id.myEditText1);
    mEditText1.setText("http://www.baidu.com");
    mWebView1 = (WebView) findViewById(R.id.myWebView1);
    button=(Button)findViewById(R.id.buttonback);
    button2=(Button)findViewById(R.id.buttonfoward);
    button.setOnClickListener(new OnClickListener()
    {
      
      @Override
      public void onClick(View arg0)
      {
        // TODO Auto-generated method stub
        if(mWebView1.canGoBack()){
          mWebView1.goBack();
        }
      }
    });
    button2.setOnClickListener(new OnClickListener()
    {
      
      @Override
      public void onClick(View v)
      {
        // TODO Auto-generated method stub
       if (mWebView1.canGoForward())
      {
        mWebView1.goForward();
      } 
      }
    });
    /*
     * 若没有设置这个东西,则载入URL时会以内置的浏览器打开网页内容
     */
    mWebView1.setWebViewClient(new WebViewClient() 
    {
      /*延含学习
      @Override
      public void onPageFinished(WebView view, String url)
      {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
      }
      */     
    });
    
    /*当按下箭头时的事件*/
    mImageButton1.setOnClickListener(new ImageButton.OnClickListener()
    {
      @Override
      public void onClick(View arg0)
      {
        // TODO Auto-generated method stub
        {                    
          mImageButton1.setImageResource(R.drawable.go_2);
          /*设定抓取EditText里面的内容*/
          String strURI = (mEditText1.getText().toString()); 
          /*?WebView里面显示网页数据*/
          /*
           * 在显示之前先判断编辑框中输入的内容是否是正确的网址
           */
          if(URLUtil.isNetworkUrl(mEditText1.getText().toString())){
          mWebView1.loadUrl(strURI);
          Log.i(TAG, "loadUrl");
          Toast.makeText
          (
            EX08_02.this,
            getString(R.string.load)+strURI,
            Toast.LENGTH_LONG).show();  
          }
          else {
            Toast.makeText(EX08_02.this, "The URL is not correct", Toast.LENGTH_SHORT).show();
          }
        }
      }      
    });
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值