Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> android編寫webview,利用list跳轉到相應的webview的Activity

android編寫webview,利用list跳轉到相應的webview的Activity

編輯:Android開發實例

編寫靜態的list,點擊進入一個webview頁面。

首先編寫listview,在main.xml文件中編寫以下代碼:

<ListView android:id="@+id/list" android:layout_width="fill_parent"
            android:layout_height="0dip" android:layout_weight="10"
            android:drawSelectorOnTop="false" />

編寫childview.xml文件。

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <ImageView android:src="@drawable/image321" android:id="@+id/image_left"
            android:adjustViewBounds="false" android:layout_width="44px"
            android:layout_height="44px" />
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:id="@+id/text3"
                android:text="@string/biaoti" android:paddingLeft="10px"
                android:paddingTop="5px" />
            <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:id="@+id/text4"
                android:text="@string/zhongjian" android:paddingLeft="10px" />
        </LinearLayout>
        <TextView android:layout_width="50px" android:layout_height="50px"
            android:paddingLeft="10px" android:id="@+id/text2" />
    </LinearLayout>
</LinearLayout>

將childview加載到ListView 中,

main類中編寫:

package com.easymorse;

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

import com.easymorse.SecondActivty;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class main extends Activity {
    /** Called when the activity is first created. */
    private ListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
//                WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        setContentView(R.layout.main);
        listView = (ListView) findViewById(R.id.list);
        listView.setBackgroundResource(R.color.baise);
        listView.setCacheColorHint(0);
        List<String> list=new ArrayList<String>();
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        ContentAdapter adapter=new ContentAdapter(this, 0, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i,
                    long l) {
                Log.i("cat", "…>>>="+adapterView.getCount()+"="+adapterView.getId()+"i="+i);
                Intent intent=new Intent();
                intent.setClass(main.this, SecondActivty.class);
                intent.putExtra("name", "hello  ni hao");
                startActivity(intent);
            }
        });
    }
    private class ContentAdapter extends ArrayAdapter<String>{
        private List<String> item;
        public ContentAdapter(Context context, int textViewResourceId,
                List<String> list) {
            super(context, textViewResourceId,list);
            item = list;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            View view = convertView;
            final String content = item.get(position);
            if(view == null){
                LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = layoutInflater.inflate(R.layout.childview, null);
            }
            if(content!=null){
                TextView textView = (TextView)view.findViewById(R.id.text2);
                textView.setText(content);
            }
            return view;
        }
    }
}

顯示效果如下圖:

點擊顯示網頁:

主要代碼有:使用Intent。

listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i,
                    long l) {
                Log.i("cat", "…>>>="+adapterView.getCount()+"="+adapterView.getId()+"i="+i);
                Intent intent=new Intent();
                intent.setClass(main.this, SecondActivty.class);
                intent.putExtra("name", "hello  ni hao");
                startActivity(intent);
            }
        });

編寫SecondActivty類:

代碼如下:

package com.easymorse;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class SecondActivty extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.secondmain);
        Intent intent=this.getIntent();
        Log.i("cat","[[[="+intent.getStringExtra("name"));
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.loadUrl("http://Henzil.easymorse.com/");
    }
    private class WebViewClientDemo extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            view.loadUrl(url);
            return true;
        }
    }
}

改動AndroidManifest.xml文件

添上這兩句,在<application></application>中添加

<activity android:name="SecondActivty"></activity>

在<application></application>外<manifest></manifest>中添加

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

uses-permission是為程序添加權限。

點擊list跳轉到相應的網站頁面,圖如下:

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