classTabs

Last-modified: 2015-03-18 (水) 12:30:11
public class Tabs extends Activity implements OnClickListener{

	TabHost th;
	TextView showResults;
	long start, stop;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabs);

		th = (TabHost) findViewById (R.id.tabhost);
		Button newTab = (Button) findViewById (R.id.bAddTab);
		Button bStart = (Button) findViewById (R.id.bStartWatch);
		Button bStop = (Button) findViewById (R.id.bStopWatch);
		showResults = (TextView) findViewById (R.id.tvShowResult);

		bStart.setOnClickListener(this);
		bStop.setOnClickListener(this);

		newTab.setOnClickListener(this);

		th.setup();

		TabSpec specs = th.newTabSpec("tag1");
		specs.setContent(R.id.tab1);
		specs.setIndicator("StopWatch");
		th.addTab(specs);

		specs = th.newTabSpec("tag2");
		specs.setContent(R.id.tab2);
		specs.setIndicator("Tab 2");
		th.addTab(specs);

		specs = th.newTabSpec("tag3");
		specs.setContent(R.id.tab3);
		specs.setIndicator("Add a Tab");
		th.addTab(specs);

		start = 0;
	}

	@Override
	public void onClick(View v) {
		// TODO 自動生成されたメソッド・スタブ
		switch(v.getId()){
		case R.id.bAddTab:
			TabSpec ourSpec = th.newTabSpec("tag1");
			ourSpec.setContent(new TabHost.TabContentFactory() {

				@Override
				public View createTabContent(String tag) {
					// TODO 自動生成されたメソッド・スタブ
					TextView text = new TextView(Tabs.this);
					text.setText("You've created a new Tab!");
					return (text);
				}
			});
			ourSpec.setIndicator("New");
			th.addTab(ourSpec);
			break;
		case R.id.bStartWatch:
			start = System.currentTimeMillis();

			break;
		case R.id.bStopWatch:
			stop = System.currentTimeMillis();
			if(start != 0){
				long result = stop - start;
				int millis = (int) result;
				int second = (int) result/1000;
				int minitues = second/60;
				millis = millis % 100;
				second = second % 60;

				showResults.setText(String.format("%d:%02d:%02d", minitues, second, millis));
			}
			break;
		}
	}
}




~
~<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

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

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

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <Button
                        android:id="@+id/bStartWatch"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Start" />

                    <Button
                        android:id="@+id/bStopWatch"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Stop" />

                    <TextView
                        android:id="@+id/tvShowResult"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                 <Button
                        android:id="@+id/bAddTab"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Add a Tab" />
                 </LinearLayout>

            </FrameLayout>
        </LinearLayout>
    </TabHost>
~</LinearLayout>