2014-01-15 27 views
0

我設計了一個應用程序,我使用Tabhost,並添加了兩個選項卡。當「長按」此選項卡時,我想重命名標籤Device1。我嘗試某種方式,但它不工作。我已經實現如下:如何重命名選項卡android使用tabhost和長時間點擊

activity_main.xml中

<?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" > 

<HorizontalScrollView 
    android:id="@+id/horScrollView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" 
    android:scrollbars="none" > 

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

<FrameLayout 
    android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <LinearLayout 
     android:id="@+id/Device1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:paddingTop="80px" > 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/Device2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:paddingTop="80px" > 

    </LinearLayout> 
</FrameLayout> 

MainActivity.java

package com.example.renametab; 
    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 
    import android.view.View; 
    import android.view.View.OnLongClickListener; 
    import android.widget.TabHost; 
    import android.widget.TextView; 
    import android.widget.TabHost.TabSpec; 

    public class MainActivity extends Activity { 
    public static TabHost tabHost; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tabHost = (TabHost) findViewById(R.id.tabHost); 
     tabHost.setup(); 

    TabSpec tabDevice1 = tabHost.newTabSpec("Device1"); 
    tabDevice1.setContent(R.id.Device1); 
    tabDevice1.setIndicator("Device1"); 

    TabSpec tabDevice2 = tabHost.newTabSpec("Device2"); 
    tabDevice2.setContent(R.id.Device2); 
    tabDevice2.setIndicator("Device2"); 
    tabHost.addTab(tabDevice1); 
    tabHost.addTab(tabDevice2); 
    tabHost.getTabWidget().getChildAt(0).setOnLongClickListener(new OnLongClickListener() { 

     @Override 
     public boolean onLongClick(View v) { 
      //How to rename tab???? 
      //I try this way but it is false 
      TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(R.id.Device1); 
      tv.setText("New Name Tab"); 
      return true; 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
} 

回答

1

使用Android Studio調試器仔細檢查,每個選項卡包含在TabWidget是帶2個孩子一個的LinearLayout:

  1. ImageView id爲android.R.id.icon;
  2. A TextView ID爲android.R.id.title

知道了這一點,我們可以找到每個標籤的標題TextView,在每個標籤上設置一個OnLongClickListener,然後使用TextView.setText()修改標題。

這是我的片段的onCreateView()方法,定義3個標籤,每個實現該功能:

 @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

     mTabHost = (TabHost) rootView.findViewById(android.R.id.tabhost); 
     mTabHost.setup(); 
     mTabHost.addTab(mTabHost.newTabSpec(TAB_1).setIndicator("Tab 1").setContent(R.id.tab_1)); 
     mTabHost.addTab(mTabHost.newTabSpec(TAB_2).setIndicator("Tab 2").setContent(R.id.tab_2)); 
     mTabHost.addTab(mTabHost.newTabSpec(TAB_3).setIndicator("Tab 3").setContent(R.id.tab_3)); 

     LinearLayout tabOne = (LinearLayout) mTabHost.getTabWidget().getChildTabViewAt(0); 
     final TextView tabOneTitle = (TextView) tabOne.findViewById(android.R.id.title); 
     LinearLayout tabTwo = (LinearLayout) mTabHost.getTabWidget().getChildTabViewAt(1); 
     final TextView tabTwoTitle = (TextView) tabTwo.findViewById(android.R.id.title); 
     LinearLayout tabThree = (LinearLayout) mTabHost.getTabWidget().getChildTabViewAt(2); 
     final TextView tabThreeTitle = (TextView) tabThree.findViewById(android.R.id.title); 

     tabOne.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View view) { 
       tabOneTitle.setText("New Tab 1"); 
       return true; 
      } 
     }); 

     tabTwo.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View view) { 
       tabTwoTitle.setText("New Tab 2"); 
       return true; 
      } 
     }); 

     tabThree.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View view) { 
       tabThreeTitle.setText("New Tab 3"); 
       return true; 
      } 
     }); 

     return rootView; 
    } 

這裏是前和截圖記錄結果後:

Before the long press on Tab 1

After the long press on Tab 1

+0

你能解釋一下當我有n個選項卡並且選項卡數量動態變化時ally –

+1

您可以像'int numTabs = mTab​​Host.getTabWidget()。getTabCount();'然後使用for循環來獲取每個選項卡,找到其標題文本,並設置其長時間點擊偵聽器。 –

1

只需使用onTabChanged事件:

TabHost th = new findViewById(android.R.id.tabhost); 
th.setOnTabChangedListener(new OnTabChangeListener() { 
    @Override 
    public void onTabChanged(final String tabId) { 
    TextView tv = (TextView) th.getCurrentView(); 
    tv.setText("New Name Tab"); 
    } 
}); 
0
<TextView 
     android:id="@+id/textView1" 
     android:layout_width="204dp" 
     android:layout_height="match_parent" 
     android:paddingLeft="10dp" 
     android:text="TAB 1"/> 

兩個線性佈局添加一個TextView在XML文件,並設置動態使用Tab切換監聽他們的文本: -

TabHost tabHost = new findViewById(android.R.id.tabHost1); 
tabHost.setOnTabChangedListener(new OnTabChangeListener() { 

@Override 
    public void onTabChanged(final String tabId) { 
    TextView textView1 = (TextView) tabHost.getCurrentView(); 
    textView1.setText("This Tab is Selected"); 
    } 
}); 
相關問題