天天看點

Android 基本元件 Button和Tab_Widget

聲明:本人部落格純屬個人學習過程中的一些仿寫的簡單練習記錄,其他論壇也有類似内容!(可能不免有錯誤之處,還望見諒,指出)

這裡是Android基本元件Button和Tab_Widget的簡單應用,通過點選Tab1、Tab2、Tab3、來切換檢視内容

圖為加入TabView按鈕的Activity和點選Tab切換效果示範

Android 基本元件 Button和Tab_Widget
Android 基本元件 Button和Tab_Widget
Android 基本元件 Button和Tab_Widget

項目名為:Tab 代碼如下: package com.tab.demo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity {      private Button button; //建構按鈕對象 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button=(Button)findViewById(R.id.tab_demo_button); //執行個體化對象,聯系到布局檔案 button.setOnClickListener(new Button.OnClickListener() { //設定監聽器,監聽點選事件 @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MainActivity.this, TabDemoActivity.class); startActivity(intent); //實作點選跳轉 } }); } } //跳轉到TabDemoActivity的代碼 

package com.tab.demo; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; public class TabDemoActivity extends TabActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost tabHost = getTabHost(); //構造一個Tab标簽容器TabHost LayoutInflater.from(this).inflate(R.layout.tab_demo, tabHost.getTabContentView(), true); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1") .setContent(R.id.view1)); //分别把構造好的标簽放入TabHost裡面 tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2") .setContent(R.id.view2)); tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Tab3") .setContent(R.id.view3)); } } MainActivity的視圖布局檔案 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" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TabView" android:id="@+id/tab_demo_button" /> </LinearLayout> TabDemoActivity 的視圖布局檔案 tab_demo xml: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent">     <TextView android:id="@+id/view1"         android:background="@drawable/one"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:text="@string/tab1"/>     <TextView android:id="@+id/view2"         android:background="@drawable/three"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:text="@string/tab2"/>     <TextView android:id="@+id/view3"         android:background="@drawable/two"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:text="@string/tab3"/> </FrameLayout> 相關資訊檔案string xml : <?xml version="1.0" encoding="utf-8"?> <resources>     <string name="hello">Hello World, Tab!</string>     <string name="app_name">Tab</string>     <string name= "tab1">Here is tab1.</string>     <string name= "tab2">Jump to Tab2.</string>     <string name= "tab3">The end is Tab3.</string> </resources> 應用配置檔案 AndroidMainfest xml : <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tab.demo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/me" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".TabDemoActivity"></activity> </application> </manifest>