需要根据选择的TAB显示活动

时间:2011-12-01 05:48:40

标签: java android android-activity

我会尽量简短地解释我的问题。我有4个标签,“月”,“周”,“日”,“今天”。这些天中的每一天都是一项单独的活动。当用户单击“今天”选项卡时,活动必须首先检查上一个活动,然后相应地显示当前日期。例如。如果用户之前单击了DAY,然后单击TODAY,则TODAY活动应显示今天的日期。 “月”“周”活动也是如此。今天将检查以前的活动并分别显示当前月份和当前周。

The TABs: each are a separate activity

以下是我用来制作标签的代码。

public class Secondactivity extends TabActivity {    
     private TabHost mTabHost;
     private void setupTabHost() {
         mTabHost = getTabHost();
     }
     setupTabHost();
     mTabHost.getTabWidget().setDividerDrawable(se.copernicus.activity.R.drawable.tab_divider);
     setupTab(new TextView(this), getString(R.string.month));
     setupTab(new TextView(this), getString(R.string.week));
     setupTab(new TextView(this), getString(R.string.day));
     setupTab(new TextView(this), getString(R.string.today));
     mTabHost.setCurrentTabByTag(getString(R.string.month));

     private void setupTab(final View view, final String tag) {
         View tabview = createTabView(mTabHost.getContext(), tag);
         if (tag.compareTo(getString(R.string.month)) == 0) {
              Intent intent = new Intent(getApplicationContext(), MonthActivity.class);
              TabSpec setContent = mTabHost.newTabSpec(getString(R.string.month)).
              setIndicator(tabview).setContent(new TabHost.TabContentFactory() {
                   public View createTabContent(String tag) {
                        return view;
                   }
              });
              setContent.setContent(intent);
              mTabHost.addTab(setContent);
         }
         if (tag.compareTo(getString(R.string.week)) == 0) {
              Intent intent = new Intent(getApplicationContext(), WeekActivity.class);
              TabSpec setContent = mTabHost.newTabSpec(getString(R.string.week)).
              setIndicator(tabview).setContent(new TabHost.TabContentFactory() {
                   public View createTabContent(String tag) {
                        return view;
                   }
              });
              setContent.setContent(intent);
              mTabHost.addTab(setContent);
         }
         if (tag.compareTo(getString(R.string.day)) == 0) {
              Intent intent = new Intent(getApplicationContext(), DayActivity.class);
              TabSpec setContent = mTabHost.newTabSpec(getString(R.string.day)).
              setIndicator(tabview).setContent(new TabHost.TabContentFactory() {
                   public View createTabContent(String tag) {
                        return view;
                   }
              });
              setContent.setContent(intent);
              mTabHost.addTab(setContent);
         }
         if (tag.compareTo(getString(R.string.today)) == 0) {
              Intent intent = new Intent(getApplicationContext(), DayActivity.class);
              TabSpec setContent = mTabHost.newTabSpec(getString(R.string.today)).
              setIndicator(tabview).setContent(new TabHost.TabContentFactory() {
                   public View createTabContent(String tag) {
                        return view;
                   }
              });
              setContent.setContent(intent);
              mTabHost.addTab(setContent);
         }
     }

     private static View createTabView(final Context context, final String text) {
              View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
              TextView tv = (TextView) view.findViewById(R.id.tabsText);
              tv.setText(text);
              return view;
     }
 }

1 个答案:

答案 0 :(得分:4)

Vinoth,        当您单击选项卡时,最好使用共享首选项。。存储共享首选项中的值。    例如,您单击日期选项卡使用以下共享的首选项

 getSharedPreferences("Test", 0).edit().putString("Check", "day").commit();

并在点击月份标签

时使用以下偏好设置
getSharedPreferences("Test", 0).edit().putString("Check", "month").commit();

并在点击周标签

时使用以下偏好设置
getSharedPreferences("Test", 0).edit().putString("Check", "week").commit();

最后当我们点击Today选项卡时,根据该值获取共享首选项值打印日,月,周.Below代码用于获取共享首选项值

String check_string= getSharedPreferences("Test", 0).getString("Check","day")
if( check_string.equalsIgnorecase("day")
{
// it is coming from day tab
}
else if ( check_string.equalsIgnorecase("month")
{
//it is coming from month tab
}else if ( check_string.equalsIgnorecase("week")
{
//it is coming from week tab
}