首页 > 代码库 > 一个android的webview的例子

一个android的webview的例子


截图如图所示。

核心部分代码

package com.example.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

	private TextView mNews;
	private TextView interview;
	private WebView mWebView; 
	
	final int RIGHT = 0;  
    final int LEFT = 1;  
    private GestureDetector gestureDetector;  
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setWebViewClient(new webViewClient()); 
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.loadUrl("http://www.itbuluoge.com/app/index.php?action=interview");     

        mNews=(TextView)findViewById(R.id.news);
        mNews.setClickable(true); //设置tv为可点击。
        mNews.setOnTouchListener(new OnTouchListener()
        {   //添加OnClickListener
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				mWebView.loadUrl("http://www.itbuluoge.com/app/index.php?action=news");     
				return false;
			}
            
        });
        
        interview=(TextView)findViewById(R.id.interview);
        interview.setClickable(true); //设置tv为可点击。
        interview.setOnTouchListener(new OnTouchListener()
        {   //添加OnClickListener
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				mWebView.loadUrl("http://www.itbuluoge.com/app/index.php?action=interview");     
				return false;
			}
            
        });
        
        gestureDetector = new GestureDetector(MainActivity.this,onGestureListener);  
        /*
        listView=(ListView)this.findViewById(R.id.listView);
      //生成动态数组,加入数据  
        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();  
        for(int i=0;i<10;i++)  
        {  
            HashMap<String, Object> map = new HashMap<String, Object>();  
            map.put("title", "百度面试经验指南 "+i);  
            map.put("datetime", "2月4号"+i);  
            listItem.add(map);  
        }
      //生成适配器的Item和动态数组对应的元素  
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源   
            R.layout.item,//ListItem的XML实现  
            //动态数组与ImageItem对应的子项          
            new String[] {"title","datetime"},   
            //ImageItem的XML文件里面的一个ImageView,两个TextView ID  
            new int[] {R.id.title,R.id.datetime}  
        );  
         
        //添加并且显示  
        listView.setAdapter(listItemAdapter);
        */
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        
        
    }

/*
	private void show() {
		// TODO Auto-generated method stub
		List<String> data = http://www.mamicode.com/new ArrayList();>

采用webview,并且处理了禁止了页面横条的效果。

下载地址:

http://download.csdn.net/detail/jianghuihong2012/7502237



IT部落格(http://www.itbuluoge.com)-快乐快速学习,专注于免费编程教程,在线编程,IT面试笔记,IT面试经验