วันพฤหัสบดีที่ 19 กันยายน พ.ศ. 2556

Android : เริ่มต้นเขียน โปรแกรม Web Connect

บทนี้ จะเป็นการทดลองเขียน Source Code และ การออกแบบหน้าจอ โปรแกรม Web Connect ซึ่งเป็นโปรแกรมสำหรับเล่น เชื่อมต่อไปยังหน้าเว็บไซต์
     เรามาดูรายละเอียด Source Code การสร้างโปรแกรมกัน
โดยเราต้องสร้างๆไฟล์หลักๆ 2 ไฟล์ คือ Main.java และ main.xml และมีการแก้ไขในส่วนของ manifest.xml

//Main.java
package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class Main extends Activity {
private WebView webView;
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
  
 webView = (WebView)findViewById(R.id.webview);
 webView.getSettings().setJavaScriptEnabled(true);
 webView.loadUrl("http://www.fitab.com/rss/hasp.html");
}
}
//--------------------------------------------------------------------------------------

//main.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"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<WebView android:id="@+id/webView1" 
android:layout_width="match_parent" 
android:layout_height="match_parent"></WebView>
</LinearLayout>

//--------------------------------------------------------------------------------------
//manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
   
 <!--  อนุญาตให้เชื่อมต่ออินเตอร์เน็ต -->
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>
//--------------------------------------------------------------------------------------

ทดลอง Run โปรแกรม