TabHost
<?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" > <TabWidget android:id="@android:id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableLayout android:id="@+id/tab1" android:layout_width="200dp" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:paddingTop="70dp" > <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="@string/promptSex" /> <RadioGroup android:id="@+id/radGSex" android:layout_width="200dp" android:layout_height="wrap_content" android:orientation="vertical" android:checkedButton="@+id/radMale"> <RadioButton android:id="@+id/radMale" android:textSize="20sp" android:text="@string/male" /> <RadioButton android:id="@+id/radFemale" android:textSize="20sp" android:text="@string/female" /> </RadioGroup> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="@string/promptAge" /> <RadioGroup android:id="@+id/radGAge" android:layout_width="200dp" android:layout_height="wrap_content" android:orientation="vertical" android:checkedButton="@+id/radAgeRng1"> <RadioButton android:id="@+id/radBtnAgeRng1" android:textSize="20sp" android:text="@string/maleAgeRng1" /> <RadioButton android:id="@+id/radBtnAgeRng2" android:textSize="20sp" android:text="@string/maleAgeRng2" /> <RadioButton android:id="@+id/radBtnAgeRng3" android:textSize="20sp" android:text="@string/maleAgeRng3" /> </RadioGroup> </TableRow> <Button android:id="@+id/btnDoSug" android:layout_width="200dp" android:layout_height="wrap_content" android:textSize="20sp" android:text="@string/promptBtnDoSug" /> <TextView android:id="@+id/txtResult1" android:layout_width="200dp" android:layout_height="wrap_content" android:textSize="20sp" android:text="@string/sugResult" /> </TableLayout> <RelativeLayout android:id="@+id/tab2" android:layout_width="400dp" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:paddingTop="70dp" > <TextView android:id="@+id/txtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/promptTitle" android:textSize="40sp" android:textColor="#FF00FF" android:textStyle="bold" android:layout_centerHorizontal="true" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" /> <TextView android:id="@+id/txtCom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/promptComPlay" android:layout_below="@id/txtTitle" android:layout_alignLeft="@id/txtTitle" android:textSize="20sp" android:layout_marginBottom="20dp" /> <TextView android:id="@+id/txtMyPlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/promptMyPlay" android:layout_below="@id/txtTitle" android:layout_alignRight="@id/txtTitle" android:textSize="20sp" android:layout_marginBottom="20dp" /> <Button android:id="@+id/btnScissors" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/playScissors" android:layout_below="@id/txtMyPlay" android:layout_alignLeft="@id/txtMyPlay" android:textSize="20sp" android:paddingLeft="15dp" android:paddingRight="15dp" /> <TextView android:id="@+id/txtComPlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:layout_below="@id/btnScissors" android:layout_alignLeft="@id/txtCom" android:textSize="30sp" android:textColor="#FF00FF" /> <Button android:id="@+id/btnStone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/playStone" android:layout_below="@id/btnScissors" android:layout_alignLeft="@id/btnScissors" android:textSize="20sp" android:paddingLeft="15dp" android:paddingRight="15dp" /> <Button android:id="@+id/btnNet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/playNet" android:layout_below="@id/btnStone" android:layout_alignLeft="@id/btnStone" android:textSize="20sp" android:paddingLeft="25dp" android:paddingRight="25dp" /> <TextView android:id="@+id/txtResult2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/result" android:layout_below="@id/btnNet" android:layout_alignLeft="@id/txtCom" android:textSize="20sp" android:textColor="#0FFFFF" android:layout_marginTop="20dp" /> </RelativeLayout> </FrameLayout> </TabHost>
javad 代码:
package tw.android;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TabHost.TabSpec;
public class Main extends Activity {
private Button btnDoSug;
private RadioGroup radGSex, radGAge;
private RadioButton radBtnAgeRng1, radBtnAgeRng2, radBtnAgeRng3;
private TextView txtResult1;
private TextView txtComPlay, txtResult2;
private Button btnScissors;
private Button btnStone;
private Button btnNet;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViewComponent();
}
private void setupViewComponent() {
//
TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec=tabHost.newTabSpec("tab1");
spec.setContent(R.id.tab1);
spec.setIndicator("婚姻建议",
getResources().getDrawable(android.R.drawable.ic_lock_idle_alarm));
tabHost.addTab(spec);
spec=tabHost.newTabSpec("tab2");
spec.setIndicator("电脑猜拳游戏",
getResources().getDrawable(android.R.drawable.ic_dialog_alert));
spec.setContent(R.id.tab2);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
//
TabWidget tabWidget = (TabWidget)tabHost.findViewById(android.R.id.tabs);
View tabView = tabWidget.getChildTabViewAt(0);
TextView tab = (TextView)tabView.findViewById(android.R.id.title);
tab.setTextSize(20);
tabView = tabWidget.getChildTabViewAt(1);
tab = (TextView)tabView.findViewById(android.R.id.title);
tab.setTextSize(20);
// 眖盉某祘Α狡籹祘Α絏
btnDoSug = (Button)findViewById(R.id.btnDoSug);
radGSex = (RadioGroup)findViewById(R.id.radGSex);
radGAge = (RadioGroup)findViewById(R.id.radGAge);
radBtnAgeRng1 = (RadioButton)findViewById(R.id.radBtnAgeRng1);
radBtnAgeRng2 = (RadioButton)findViewById(R.id.radBtnAgeRng2);
radBtnAgeRng3 = (RadioButton)findViewById(R.id.radBtnAgeRng3);
txtResult1 = (TextView)findViewById(R.id.txtResult1);
// 砞﹚ㄆンlistener
btnDoSug.setOnClickListener(btnDoSugOnClick);
radGSex.setOnCheckedChangeListener(radGSexOnCheChanLis);
// 眖筿福瞦笴栏祘Α狡籹祘Α絏
txtComPlay = (TextView)findViewById(R.id.txtComPlay);
txtResult2 = (TextView)findViewById(R.id.txtResult2);
btnScissors = (Button)findViewById(R.id.btnScissors);
btnStone = (Button)findViewById(R.id.btnStone);
btnNet = (Button)findViewById(R.id.btnNet);
btnScissors.setOnClickListener(btnScissorsLin);
btnStone.setOnClickListener(btnStoneLin);
btnNet.setOnClickListener(btnNetLin);
}
private RadioGroup.OnCheckedChangeListener radGSexOnCheChanLis = new RadioGroup.OnCheckedChangeListener () {
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if (checkedId == R.id.radMale) {
radBtnAgeRng1.setText(getString(R.string.maleAgeRng1));
radBtnAgeRng2.setText(getString(R.string.maleAgeRng2));
radBtnAgeRng3.setText(getString(R.string.maleAgeRng3));
}
else if (checkedId == R.id.radFemale) {
radBtnAgeRng1.setText(getString(R.string.femaleAgeRng1));
radBtnAgeRng2.setText(getString(R.string.femaleAgeRng2));
radBtnAgeRng3.setText(getString(R.string.femaleAgeRng3));
}
}
};
private Button.OnClickListener btnDoSugOnClick = new Button.OnClickListener() {
public void onClick(View v) {
// 秙璶磅︽祘Α絏
int iCheckedRadBtn = radGSex.getCheckedRadioButtonId();
String strSug = getString(R.string.sugResult);
switch (iCheckedRadBtn)
{
case R.id.radMale:
switch (radGAge.getCheckedRadioButtonId())
{
case R.id.radBtnAgeRng1:
strSug += getString(R.string.sugNotHurry);
break;
case R.id.radBtnAgeRng3:
strSug += getString(R.string.sugGetMarried);
break;
default:
strSug += getString(R.string.sugFindCouple);
}
break;
case R.id.radFemale:
switch (radGAge.getCheckedRadioButtonId())
{
case R.id.radBtnAgeRng1:
strSug += getString(R.string.sugNotHurry);
break;
case R.id.radBtnAgeRng3:
strSug += getString(R.string.sugGetMarried);
break;
default:
strSug += getString(R.string.sugFindCouple);
}
break;
}
txtResult1.setText(strSug);
}
};
private Button.OnClickListener btnScissorsLin = new Button.OnClickListener() {
public void onClick(View v) {
// ∕﹚筿福.
int iComPlay = (int)(Math.random()*3 + 1);
// 1 芭, 2 ホ繷, 3 ガ.
if (iComPlay == 1) {
txtComPlay.setText(R.string.playScissors);
txtResult2.setText(getString(R.string.result) +
getString(R.string.playerDraw));
}
else if (iComPlay == 2) {
txtComPlay.setText(R.string.playStone);
txtResult2.setText(getString(R.string.result) +
getString(R.string.playerLose));
}
else {
txtComPlay.setText(R.string.playNet);
txtResult2.setText(getString(R.string.result) +
getString(R.string.playerWin));
}
}
};
private Button.OnClickListener btnStoneLin = new Button.OnClickListener() {
public void onClick(View v) {
// ∕﹚筿福.
int iComPlay = (int)(Math.random()*3 + 1);
// 1 芭, 2 ホ繷, 3 ガ.
if (iComPlay == 1) {
txtComPlay.setText(R.string.playScissors);
txtResult2.setText(getString(R.string.result) +
getString(R.string.playerWin));
}
else if (iComPlay == 2) {
txtComPlay.setText(R.string.playStone);
txtResult2.setText(getString(R.string.result) +
getString(R.string.playerDraw));
}
else {
txtComPlay.setText(R.string.playNet);
txtResult2.setText(getString(R.string.result) +
getString(R.string.playerLose));
}
}
};
private Button.OnClickListener btnNetLin = new Button.OnClickListener() {
public void onClick(View v) {
// ∕﹚筿福.
int iComPlay = (int)(Math.random()*3 + 1);
// 1 芭, 2 ホ繷, 3 ガ.
if (iComPlay == 1) {
txtComPlay.setText(R.string.playScissors);
txtResult2.setText(getString(R.string.result) +
getString(R.string.playerLose));
}
else if (iComPlay == 2) {
txtComPlay.setText(R.string.playStone);
txtResult2.setText(getString(R.string.result) +
getString(R.string.playerWin));
}
else {
txtComPlay.setText(R.string.playNet);
txtResult2.setText(getString(R.string.result) +
getString(R.string.playerDraw));
}
}
};
}