304-頁籤功能(行動裝置進階應用)

1. AndroidManifest.xml
  • 重點程式碼
 <activity android:name=".Webpage" />
 <activity android:name=".Webpageyahoo" />
 <uses-permission android:name="android.permission.INTERNET" />
 

2. main.xml

 <?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </FrameLayout>
    </LinearLayout>

</TabHost>


3. webview.xml

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

    <WebView
        android:id="@+id/webview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


4. webviewyahoo.xml

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

    <WebView
        android:id="@+id/webviewyahoo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


5. GAD03.java

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class GAD03 extends TabActivity {

    TabHost mTabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTabHost = getTabHost();

        TabSpec googleTabSpec = mTabHost.newTabSpec("tab_test_google");
        googleTabSpec.setIndicator("Google");
        Intent googleIntent = new Intent(this, Webpage.class);
        googleTabSpec.setContent(googleIntent);

        TabSpec yahootabSpec = mTabHost.newTabSpec("tab_test_yahoo");
        yahootabSpec.setIndicator("Yahoo");
        Intent yahooIntent = new Intent(this, Webpageyahoo.class);
        yahootabSpec.setContent(yahooIntent);

        mTabHost.addTab(googleTabSpec);
        mTabHost.addTab(yahootabSpec);
        mTabHost.setCurrentTab(0);
    }
}

 
6. Webpage.java

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Webpage extends Activity {

    WebView webview;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        webview = (WebView) findViewById(R.id.webview);
        webview.loadUrl("http://www.google.com.tw/");
        webview.setWebViewClient(new GoogleWebViewClient());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    class GoogleWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            view.loadUrl(url);
            return true;
        }
    }
}


7. Webpageyahoo.java

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Webpageyahoo extends Activity {
    WebView webview;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webviewyahoo);
        webview = (WebView) findViewById(R.id.webviewyahoo);
        webview.loadUrl("http://tw.yahoo.com/");
        webview.setWebViewClient(new YahooWebViewClient());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    class YahooWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            view.loadUrl(url);
            return true;
        }
    }
}

沒有留言: