首页 > 移动平台 > 详细

一个android的webview的例子

时间:2014-06-16 22:21:43      阅读:516      评论:0      收藏:0      [点我收藏+]

bubuko.com,布布扣


截图如图所示。

核心部分代码

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 = new ArrayList<String>();
		
		List<HashMap<String,Object>> data=new ArrayList<HashMap<String,Object>>();
		for(news str :list)
		{
			HashMap<String,Object> item= new HashMap<String,Object>();
			item.put("title", str.title);
			item.put("datetime", str.datetime);
		}
    	SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.item,new String[]{"title","datetime"},new int[]{R.id.title,R.id.datetime});
    	
    	listView.setAdapter(adapter);
    }
*/

    private GestureDetector.OnGestureListener onGestureListener =   
            new GestureDetector.SimpleOnGestureListener() {  
            @Override  
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
                    float velocityY) {  
                float x = e2.getX() - e1.getX();  
                float y = e2.getY() - e1.getY();  
      
                if (x > 0) {  
                    doResult(RIGHT);  
                } else if (x < 0) {  
                    doResult(LEFT);  
                }  
                return true;  
            }  
        };  
        
        public boolean onTouchEvent(MotionEvent event) {  
            return gestureDetector.onTouchEvent(event);  
        }  
      
        public void doResult(int action) {  
      
        	if(action==0)//右边
        	{
        		this.mWebView.loadUrl("http://www.itbuluoge.com/app/index.php?action=interview");
        	}
        	else
        	{
        		this.mWebView.loadUrl("http://www.itbuluoge.com/app/index.php?action=news");
        	}
           
        }  
	private List<String> getData() {
		// TODO Auto-generated method stub
		List<String> data = new ArrayList<String>();
        data.add("测试数据1");
        data.add("测试数据2");
        data.add("测试数据3");
        data.add("测试数据4");
         
        return data;
	}

	@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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
    
    class webViewClient extends WebViewClient{ 

        //重写shouldOverrideUrlLoading方法,使点击链接后不使用其他的浏览器打开。 

     @Override 

     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

         view.loadUrl(url); 

         //如果不需要其他对点击链接事件的处理返回true,否则返回false 

         return true; 

     } 

         

    } 

}

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

下载地址:

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



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

一个android的webview的例子,布布扣,bubuko.com

一个android的webview的例子

原文:http://blog.csdn.net/itbuluoge/article/details/31015675

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!