以下是本人在处理ActivityGroup中各个子Activity切换和旋转屏幕时,各个子Activity的相关处理,接触android时间不长,有什么不周到的地方还请见谅,欢迎高手指点有哪些地方做的不好,万分感谢。

//继承ActivityGroupLocalActivityManager
public class MainTabActivity extends ActivityGroup {
//定义LocalActivityManager来管理ActivityGroup中的子Activity
private LocalActivityManager lm;
//子Activity之间切换的按钮
private Button clock;
private Button worldclock;
private Button alarm;
private Button stopwatch;
private Button timer;
//装载子Activity布局的LinearLayout
private LinearLayout displayLayout;
//唯一标识子Activity的ID
private static final String CLOCK_DETAIL_PORT = "CLOCK_DETAIL_PORT";
private static final String WORLDCLOCK_DETAIL_PORT = "WORLDCLOCK_DETAIL_PORT";
private static final String ALARMCLOCK_DETAIL_PORT = "ALARMCLOCK_DETAIL_PORT";
private static final String STOPWATCH_DETAIL_PORT = "STOPWATCH_DETAIL_PORT";
private static final String TIMER_DETAIL_PORT = "TIMER_DETAIL_PORT";
//标识当前按钮的显示效果
private boolean isClock = false;
private boolean isWorldClock = false;
private boolean isAlarmClock = false;
private boolean isStopwatch = false;
private boolean isTimer = false;
//记录当前子Activity的ID
private static String myDisplayID;
//向子Activity发送广播的action
public static final String WORLDCLOCK_INTENT = "worldclock";
public static final String ALARMCLOCK_INTENT = "alarmclock";

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if(lm == null)
//获取LocalActivityManager
lm = getLocalActivityManager();
setContentView(R.layout.maintab);
initView();
//设置进入模块默认显示的内容
if(getActivity(CLOCK_DETAIL_PORT)== null){
Intent intent = new Intent(MainTabActivity.this,
ClockActivity.class);
chooseActivity(intent,CLOCK_DETAIL_PORT);

}else{
resumeActivity(getActivity(CLOCK_DETAIL_PORT),true);
}
//设置各个按钮的显示状态
selectClock(true);
selectWorldClock(false);
selectAlarmClock(false);
selectStopWatch(false);
selectTimer(false);
//记录当前Activity的ID
myDisplayID = CLOCK_DETAIL_PORT;
}

private void initView() {
//初始化控件

clock = (Button) findViewById(R.id.clock);
worldclock = (Button) findViewById(R.id.worldclock);
alarm = (Button) findViewById(R.id.alarmclock);
stopwatch = (Button) findViewById(R.id.stopwatch);
timer = (Button) findViewById(R.id.timer);
displayLayout = (LinearLayout) findViewById(R.id.display);
onClickClockBtn();
onClickWroldclockBtn();
onClickAlarmBtn();
onClickStopwatchBtn();
onClickTimerBtn();
}
//定义ClockActivity按钮的点击事件

private void onClickClockBtn() {
clock.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
selectClock(true);
selectWorldClock(false);
selectAlarmClock(false);
selectStopWatch(false);
selectTimer(false);
if(getActivity(CLOCK_DETAIL_PORT)== null){
Intent intent = new Intent(MainTabActivity.this,
ClockActivity.class);
chooseActivity(intent,CLOCK_DETAIL_PORT);

}else{
resumeActivity(getActivity(CLOCK_DETAIL_PORT),true);
}
myDisplayID = CLOCK_DETAIL_PORT;
}
});
}
//定义WorldClockActivity按钮的点击事件

private void onClickWroldclockBtn() {
worldclock.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
selectClock(false);
selectWorldClock(true);
selectAlarmClock(false);
selectStopWatch(false);
selectTimer(false);
if(getActivity(WORLDCLOCK_DETAIL_PORT)== null){
Intent intent = new Intent(MainTabActivity.this,
WorldClockActivity.class);
chooseActivity(intent,WORLDCLOCK_DETAIL_PORT);

}else{
resumeActivity(getActivity(WORLDCLOCK_DETAIL_PORT),true);
}
myDisplayID = WORLDCLOCK_DETAIL_PORT;
}
});
}
//定义AlarmClockActivity按钮的点击事件

private void onClickAlarmBtn() {
alarm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
selectClock(false);
selectWorldClock(false);
selectAlarmClock(true);
selectStopWatch(false);
selectTimer(false);
if(getActivity(ALARMCLOCK_DETAIL_PORT)== null){
Intent intent = new Intent(MainTabActivity.this,
AlarmClockActivity.class);
chooseActivity(intent,ALARMCLOCK_DETAIL_PORT);

}else{
resumeActivity(getActivity(ALARMCLOCK_DETAIL_PORT),false);
}
myDisplayID = ALARMCLOCK_DETAIL_PORT;
}
});
}
//定义stopwatchActivity按钮的点击事件

private void onClickStopwatchBtn() {
stopwatch.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
selectClock(false);
selectWorldClock(false);
selectAlarmClock(false);
selectStopWatch(true);
selectTimer(false);
if(getActivity(STOPWATCH_DETAIL_PORT)== null){
Intent intent = new Intent(MainTabActivity.this,
StopWatchActivity.class);
chooseActivity(intent,STOPWATCH_DETAIL_PORT);

}else{
resumeActivity(getActivity(STOPWATCH_DETAIL_PORT),false);
}
myDisplayID = STOPWATCH_DETAIL_PORT;
}
});
}
//定义TimerActivity按钮的点击事件

private void onClickTimerBtn() {
timer.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
selectClock(false);
selectWorldClock(false);
selectAlarmClock(false);
selectStopWatch(false);
selectTimer(true);
if(getActivity(TIMER_DETAIL_PORT)== null){
Intent intent = new Intent(MainTabActivity.this,
TimerActivity.class);
chooseActivity(intent,TIMER_DETAIL_PORT);

}else{
resumeActivity(getActivity(TIMER_DETAIL_PORT),false);
}
myDisplayID = TIMER_DETAIL_PORT;
}
});
}
//启动子Activity
private void chooseActivity(Intent intent, String activityId) {
if (intent == null) {
return;
}
Window w = lm.startActivity(activityId, intent);
if(w != null){
displayLayout.removeAllViews();
displayLayout.addView(w.getDecorView(), LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
}
}

@Override
protected void onResume() {
super.onResume();
}





//定义子Activity中需要的menu
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
if (getActivity(myDisplayID) instanceof WorldClockActivity) {
menu.add(0, 0, 0, R.string.worldclcok_add).setIcon(R.drawable.option_add);
menu.add(0, 1, 0, R.string.worldclock_del).setIcon(R.drawable.option_delete);
menu.add(0, 2, 0, R.string.worldclock_dst).setIcon(R.drawable.option_dts_setting);
}
if (getActivity(myDisplayID) instanceof AlarmClockActivity) {
menu.add(0, 3, 0, R.string.add_alarm).setIcon(R.drawable.option_add);
menu.add(0, 4, 0, R.string.del_alarm).setIcon(R.drawable.option_delete);
menu.add(0, 5, 0, R.string.del_all).setIcon(R.drawable.option_delete_all);
menu.add(0, 6, 0, R.string.all_on).setIcon(R.drawable.option_all_on);
menu.add(0, 7, 0, R.string.all_off).setIcon(R.drawable.option_all_off);
}
return true;
}


//menu各个item的点击处理
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case 0:
Intent intent0 = new Intent();
intent0.setAction(WORLDCLOCK_INTENT);
intent0.putExtra("menuid", "add");
this.sendBroadcast(intent0);

return true;
case 1:
Intent intent1 = new Intent();
intent1.setAction(WORLDCLOCK_INTENT);
intent1.putExtra("menuid", "del");
this.sendBroadcast(intent1);
return true;

case 2:
Intent intent2 = new Intent();
intent2.setAction(WORLDCLOCK_INTENT);
intent2.putExtra("menuid", "dts");
this.sendBroadcast(intent2);
return true;
case 3:
Intent intent3 = new Intent();
intent3.setAction(ALARMCLOCK_INTENT);
intent3.putExtra("menuid", "add_alarm");
this.sendBroadcast(intent3);

return true;
case 4:
Intent intent4 = new Intent();
intent4.setAction(ALARMCLOCK_INTENT);
intent4.putExtra("menuid", "del_alarm");
this.sendBroadcast(intent4);
return true;

case 5:
Intent intent5 = new Intent();
intent5.setAction(ALARMCLOCK_INTENT);
intent5.putExtra("menuid", "del_all");
this.sendBroadcast(intent5);
return true;
case 6:
Intent intent6 = new Intent();
intent6.setAction(ALARMCLOCK_INTENT);
intent6.putExtra("menuid", "all_on");
this.sendBroadcast(intent6);
return true;

case 7:
Intent intent7 = new Intent();
intent7.setAction(ALARMCLOCK_INTENT);
intent7.putExtra("menuid", "all_off");
this.sendBroadcast(intent7);
return true;
}

return false;
}
//旋转屏幕处理事件
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.maintab);
initView();
reusmeAllView();
Activity tmpActivity = getActivity(myDisplayID);
if (tmpActivity != null){
resumeActivity(tmpActivity,true);

}
if(!myDisplayID.equals(CLOCK_DETAIL_PORT)){
tmpActivity = getActivity(CLOCK_DETAIL_PORT);
if(tmpActivity != null){
if (tmpActivity instanceof RefreshChild) {
((RefreshChild) tmpActivity).onConfigurationChangedFromParent(true);
}
}
}
if(!myDisplayID.equals(WORLDCLOCK_DETAIL_PORT)){
tmpActivity = getActivity(WORLDCLOCK_DETAIL_PORT);
if(tmpActivity != null){
if (tmpActivity instanceof RefreshChild) {
((RefreshChild) tmpActivity).onConfigurationChangedFromParent(true);
}
}
}
if(!myDisplayID.equals(ALARMCLOCK_DETAIL_PORT)){
tmpActivity = getActivity(ALARMCLOCK_DETAIL_PORT);
if(tmpActivity != null){
if (tmpActivity instanceof RefreshChild) {
((RefreshChild) tmpActivity).onConfigurationChangedFromParent(true);
}
}
}
if(!myDisplayID.equals(STOPWATCH_DETAIL_PORT)){
tmpActivity = getActivity(STOPWATCH_DETAIL_PORT);
if(tmpActivity != null){
if (tmpActivity instanceof RefreshChild) {
((RefreshChild) tmpActivity).onConfigurationChangedFromParent(true);
}
}
}
if(!myDisplayID.equals(TIMER_DETAIL_PORT)){
tmpActivity = getActivity(TIMER_DETAIL_PORT);
if(tmpActivity != null){
if (tmpActivity instanceof RefreshChild) {
((RefreshChild) tmpActivity).onConfigurationChangedFromParent(true);
}
}
}
}

//旋转屏幕时设置各个按钮的显示状态
private void reusmeAllView() {
if (isClock) {
selectClock(true);
selectWorldClock(false);
selectAlarmClock(false);
selectStopWatch(false);
selectTimer(false);
}
if (isWorldClock) {
selectClock(false);
selectWorldClock(true);
selectAlarmClock(false);
selectStopWatch(false);
selectTimer(false);
}
if (isAlarmClock) {
selectClock(false);
selectWorldClock(false);
selectAlarmClock(true);
selectStopWatch(false);
selectTimer(false);
}
if (isStopwatch) {
selectClock(false);
selectWorldClock(false);
selectAlarmClock(false);
selectStopWatch(true);
selectTimer(false);
}
if (isTimer) {
selectClock(false);
selectWorldClock(false);
selectAlarmClock(false);
selectStopWatch(false);
selectTimer(true);
}
}
//重新加载子Activity的内容
private void resumeActivity(Activity activity,boolean isDestroy) {
if (activity instanceof RefreshChild) {
((RefreshChild) activity).onConfigurationChangedFromParent(isDestroy);
}
View activityView = null;
try {
activityView = activity.getWindow().getDecorView();
} catch (Exception e) {
}

ViewParent parentView = activityView.getParent();
if (parentView != null && parentView instanceof ViewGroup) {
((ViewGroup) parentView).removeAllViews();
}

if (activityView != null && displayLayout != null) {
displayLayout.removeAllViews();
displayLayout.addView(activityView);
}

}
//获得指定ID的activity
private Activity getActivity(String myActivityId) {
if (lm != null)
return lm.getActivity(myActivityId);
else
return null;

}
//定义公共接口,子Activity继承接口,处理旋转屏幕事件
public interface RefreshChild {
void onConfigurationChangedFromParent(boolean isDestroy);
}
//定义ClockActivity按钮被点击后显示状态

private void selectClock(boolean b) {
isClock = b;
clock.setBackgroundDrawable(getResources().getDrawable(
b ? R.drawable.tab_clock_press : R.drawable.tab_clock));
clock.setClickable(!b);
}
//定义WroldClockActivity按钮被点击后显示状态

private void selectWorldClock(boolean b) {
isWorldClock = b;
worldclock.setBackgroundDrawable(getResources()
.getDrawable(
b ? R.drawable.tab_worldclock_press
: R.drawable.tab_worldclock));
worldclock.setClickable(!b);
}
//定义AlarmClockActivity按钮被点击后显示状态
private void selectAlarmClock(boolean b) {
isAlarmClock = b;
alarm.setBackgroundDrawable(getResources().getDrawable(
b ? R.drawable.tab_alarm_press : R.drawable.tab_alarm));
alarm.setClickable(!b);
}
//定义StopwatchActivity按钮被点击后显示状态
private void selectStopWatch(boolean b) {
isStopwatch = b;
stopwatch.setBackgroundDrawable(getResources().getDrawable(
b ? R.drawable.tab_stopwatch_press : R.drawable.tab_stopwatch));
stopwatch.setClickable(!b);
}
//定义TimerActivity按钮被点击后显示状态
private void selectTimer(boolean b) {
isTimer = b;
timer.setBackgroundDrawable(getResources().getDrawable(
b ? R.drawable.tab_timer_press : R.drawable.tab_timer));
timer.setClickable(!b);
}

}

xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<RelativeLayout android:id="@+id/head_tab_view"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:id="@+id/clock" android:layout_width="120px"
android:layout_height="98px" android:background="@drawable/tab_alarm"
android:layout_centerVertical="true" android:gravity="bottom|center_horizontal" />
<Button android:id="@+id/worldclock" android:layout_width="120px"
android:layout_height="98px" android:background="@drawable/tab_worldclock"
android:layout_toRightOf="@id/clock" android:layout_centerVertical="true"
android:gravity="bottom|center_horizontal" />
<Button android:id="@+id/alarmclock" android:layout_width="120px"
android:layout_height="98px" android:background="@drawable/tab_alarm"
android:layout_toRightOf="@id/worldclock" android:layout_centerVertical="true"
android:gravity="bottom|center_horizontal" />
<Button android:id="@+id/stopwatch" android:layout_width="120px"
android:layout_height="98px" android:background="@drawable/tab_stopwatch"
android:layout_toRightOf="@id/alarmclock" android:layout_centerVertical="true"
android:gravity="bottom|center_horizontal" />
<Button android:id="@+id/timer" android:layout_width="120px"
android:layout_height="98px" android:background="@drawable/tab_timer"
android:layout_toRightOf="@id/stopwatch" android:layout_centerVertical="true"
android:gravity="bottom|center_horizontal" />
</RelativeLayout>

<LinearLayout android:layout_width="fill_parent" android:id="@+id/display"
android:layout_height="fill_parent" android:layout_weight="1">
</LinearLayout>
</LinearLayout>

更多相关文章

  1. android分享功能的实现
  2. 如何在Windows下搭建Android开发环境(转)
  3. Buttons in button bars should be borderless;
  4. Android三种实现自定义ProgressBar的方式介绍
  5. Android(安卓)studio下运行百度地图demo
  6. Android(安卓)ImageButton Example 图片按钮
  7. 使用SpannableString设置部分文字大小、颜色、超链接、点击事件
  8. Android(安卓)使用Java8新特性之Lambda expression 附命令者模式
  9. Android(安卓)DatePicker的简单自定义

随机推荐

  1. Android平台实现双向证书(https)验证简单
  2. 【android x86 5.1】system/core/目录下R
  3. Android(安卓)LocalBroadcastReceiver本
  4. Android(安卓)Studio录制手机屏幕并制作G
  5. android仿腾讯安全管家首页抽屉效果
  6. android版PDA通过USB与.net应用程序通讯,
  7. Android音量调节的实现(RingtoneManager
  8. Android自学笔记-17-广播接收者(Broadcast
  9. 实践中探索Android智能手机系统------APK
  10. Android的死机、重启问题分析方法