Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 一個android的webview的例子

一個android的webview的例子

編輯:關於Android編程

\


<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+vdjNvMjnzbzL+cq+oaM8L3A+CjxwPrrL0MSyv7fWtPrC6zwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">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> listItem = new ArrayList>(); for(int i=0;i<10;i++) { HashMap map = new HashMap(); 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 data = new ArrayList(); List> data=new ArrayList>(); for(news str :list) { HashMap item= new HashMap(); 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 getData() { // TODO Auto-generated method stub List data = new ArrayList(); 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面試經驗

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved