Tutorial :: Android :: ตัวอย่างการใช้งาน :: ตัวอย่างการคืนค่า Tag ของ Tab ปัจจุบันที่แสดงอยู่ใน TabHost
ในบทความนี้ จะแสดงให้เห็นถึงตัวอย่างการคืนค่า Tag ของ Tab ปัจจุบันที่แสดงอยู่ใน TabHost โดยจะแสดงให้เห็นถึงการเขียนคำสั่งภายใน Class Activity, การเขียนคำสั่งภายในไฟล์ XML Layout, และการประกาศ Activity ไว้ภายในไฟล์ AndroidManifest.xml ดังนี้
การเขียนคำสั่งภายใน Class Activity
- package nutt.me.activity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TabHost;
- import android.widget.Toast;
- public class MainActivity extends Activity
- {
- public void onCreate ( Bundle savedInstanceState )
- {
- super.onCreate ( savedInstanceState );
- this.setContentView ( R.layout.activity_main );
- TabHost tabHost = ( TabHost ) this.findViewById ( R.id.tabhost );
- tabHost.setup ( );
- TabHost.TabSpec tab_analogClock = tabHost.newTabSpec ( "analogclock" );
- tab_analogClock.setIndicator ( "Analog" );
- tab_analogClock.setContent ( R.id.analogclock );
- tabHost.addTab ( tab_analogClock );
- TabHost.TabSpec tab_digitalClock = tabHost.newTabSpec ( "digitalclock" );
- tab_digitalClock.setIndicator ( "Digital" );
- tab_digitalClock.setContent ( R.id.digitalclock );
- tabHost.addTab ( tab_digitalClock );
- String currentTag = tabHost.getCurrentTabTag ( );
- Toast toast = Toast.makeText ( this, "Current Tag of Tab :: " + currentTag, Toast.LENGTH_LONG );
- toast.show ( );
- }
- }
จากคำสั่งข้างต้น สามารถอธิบายได้ว่า คำสั่ง TabHost tabHost = ( TabHost ) this.findViewById ( R.id.tabhost ); ในบรรทัดที่ 17 เป็นการสร้าง Object TabHost โดยการไปค้นหามาจาก XML Layout และคำสั่ง String currentTag = tabHost.getCurrentTabTag ( ); ในบรรทัดที่ 40 เป็นการคืนค่า Tag ของ Tab ปัจจุบันที่แสดงอยู่ใน TabHost แล้วนำค่าที่ได้มาเก็บไว้ที่ตัวแปรประเภท String
การเขียนคำสั่งภายในไฟล์ XML Layout
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/tabhost"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <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" >
- <AnalogClock
- android:id="@+id/analogclock"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
- <DigitalClock
- android:id="@+id/digitalclock"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
- </FrameLayout>
- </LinearLayout>
- </TabHost>
จากคำสั่งข้างต้น สามารถอธิบายได้ว่า element <TabHost> มี id คือ tabhost (ประกาศไว้ในบรรทัดที่ 3) และ element <AnalogClock> มี id คือ analogclock (ประกาศไว้ในบรรทัดที่ 23) และ element <DigitalClock> มี id คือ digitalclock (ประกาศไว้ในบรรทัดที่ 28) ซึ่ง id เหล่านี้ถูกใช้อ้างอิงใน Class Activity ข้างต้น
การประกาศ Activity ไว้ภายในไฟล์ AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest ... >
- <application ... >
- <activity android:name="nutt.me.activity.MainActivity" ... >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
จากคำสั่งข้างต้น สามารถอธิบายได้ว่า มีการประกาศ Activity ชื่อ MainActivity ที่อยู่ใน Package nutt.me.activity ไว้ภายในไฟล์ AndroidManifest.xml ในบรรทัดที่ 4
หน้าจอแสดงผลของ ตัวอย่างการคืนค่า Tag ของ Tab ปัจจุบันที่แสดงอยู่ใน TabHost จากการเขียนคำสั่งข้างต้น

จากรูปแสดงหน้าจอของ MainActivity
0 Comment
- Have no comment.