2015-09-28 75 views
0

我在我的仪表盘,即3个标签,如何在android中为仪表板选项卡设置唯一的ID?

  1. 邀请
  2. 事件
  3. GROUPCHAT

我使用添加编程方式将所有这些选项卡,在我的布局代码id:tabContent我所有的标签。我Userdashboard.xml代码如下,

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

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="-4dp" 
     android:layout_weight="0" /> 

</LinearLayout> 

在下面的代码的所有标签页归类为 “tabHost”。现在我需要为所有三个选项卡设置唯一的ID,但我不知道如何设置唯一的ID为帮助我,请提前致谢。

public class UserDashBoardActivity extends ActionBarActivity { 

    /** Called when the activity is first created. */ 
    private static final String TAB_1_TAG = "Invitation"; 
    private static final String TAB_2_TAG = "Event"; 
    private static final String TAB_3_TAG = "GroupChat"; 
    private FragmentTabHost tabHost; 

    private Context context; 
    private SharedPreferences sharedpreferences; 
    private Gson gson = new Gson(); 
    private Menu menu; 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     AppActivityStatus.setActivityStarted(); 
     AppActivityStatus.setActivityContext(context); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     AppActivityStatus.setActivityStoped(); 

    } 

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

    @Override 
    protected void onStop() { 
     super.onStop(); 
     AppActivityStatus.setActivityStoped(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     super.onCreateOptionsMenu(menu); 
     this.menu=menu; 
     getMenuInflater().inflate(R.menu.menu_user_dash_board, menu); 
     return true; 
     //return super.onCreateOptionsMenu(menu); 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.user_dash_board); 
     context = getApplicationContext(); 
     sharedpreferences = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, 
       Context.MODE_PRIVATE); 
     // Get TabHost Reference 
     tabHost= (FragmentTabHost) findViewById(android.R.id.tabhost); 
     tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 

     **Invitation,Event,Groupchat tabs** are added here  

    tabHost.addTab(tabHost.newTabSpec(TAB_1_TAG).setIndicator("Invitation"), InvitationFragment.class, null); 
    tabHost.addTab(tabHost.newTabSpec(TAB_2_TAG).setIndicator("Event"), OccasionFragment.class, null); 
    tabHost.addTab(tabHost.newTabSpec(TAB_3_TAG).setIndicator("GroupChat"), GroupChatFragment.class, null); 

     //invitation tab highlighted by default 
     tabHost.getTabWidget().setCurrentTab(0); 
     tabHost.getTabWidget().getChildAt(0).setBackgroundColor(getResources().getColor(R.color.Orange)); 
     tabHost.getTabWidget().getChildAt(1).setBackgroundColor(getResources().getColor(R.color.scandal)); 
     tabHost.getTabWidget().getChildAt(2).setBackgroundColor(getResources().getColor(R.color.scandal)); 


     //onTabChangedListener added for move one tab to others 

     tabHost.setOnTabChangedListener(new OnTabChangeListener() { 

      @Override 
      public void onTabChanged(String arg0) { 
        setTabColor(tabHost); 
      } 
     }); 


    } 
     if(tabHost.getCurrentTab()==0) 
     tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange));//1st tab selected 
     else if(tabHost.getCurrentTab()==1) 
     tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange)); //2nd tab selected 
     else if(tabHost.getCurrentTab()==2) 
     tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange)); //3rd tab selected 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 
     // noinspection SimplifiableIfStatement 
     if (id == R.id.account_settings) { 
      Intent userSettingIntent = new Intent(getApplicationContext(),ActivityUserSettings.class); 
      userSettingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(userSettingIntent); 
      return true; 
     } 

     if (id == R.id.profile) { 
      Intent profileIntent = new Intent(getApplicationContext(),ImageUploadActivity.class); 
      profileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(profileIntent); 
      return true; 
     } 

     if(id == R.id.create_occasion){ 

      Intent occasionAct = new Intent(getApplicationContext(), OccasionActivity.class); 
      // Clears History of Activity 
      occasionAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(occasionAct); 

     } 
     return super.onOptionsItemSelected(item); 
    } 

} 

回答

0

tabHost.newTabSpec()的调用采用String作为标记。在你的情况下,这些都是TAB_1_TAGTAB_2_TAGTAB_3_TAG,所以这些分别是你为每个标签唯一的ID。

您可以在此识别onTabChanged(String arg0)中的选定选项卡arg0是所选标签的名称。

此外,您可以使用tabHost.getCurrentTabTag()按标签,而不是tabHost.getCurrentTab()它是由标签位置来识别标签。