2014-01-15 1 views
0

나는 Tabhost를 사용하고 있고 두 개의 탭을 추가했다는 점에서 앱을 디자인했습니다. 이 탭을 "길게 누르십시오"때 이름 바꾸기 탭 Device1을 원합니다. 나는 어떤 방법을 시도하지만 작동하지 않습니다.탭의 이름을 바꾸는 방법 android use tabhost and long click

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

안드로이드 Studio 디버거를 사용하여 정밀 조사에를 각 탭에 포함 TabWidget에서 두 아이들과의 LinearLayout입니다 :

  1. 아이디 android.R.id.icon 가진 ImageView은;
  2. android.R.id.titleTextView입니다.

각 탭의 제목 TextView를 찾고 각 탭에서 OnLongClickListener를 설정 한 다음 TextView.setText()를 사용하여 제목을 수정할 수 있습니다.

Before the long press on Tab 1

After the long press on Tab 1

: 여기

 @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; 
    } 

전과 스크린 문서화 결과 후에이다 : 여기

각이 기능을 구현하는 3 개의 탭을 정의 내 프래그먼트 onCreateView() 메소드의
+0

n 개의 탭이 있고 탭 수가 동적으로 변경 될 때 어떻게 동일한 작업을 수행 할 수 있는지 설명 할 수 있습니까? ally –

+1

'int numTabs = mTabHost.getTabWidget(). getTabCount();'과 같은 것을 할 수 있으며 for 루프를 사용하여 각 탭을 가져 와서 제목 텍스트를 찾고 긴 클릭 리스너를 설정합니다. –

1

는 단순히 onTabChanged 이벤트 사용 :

,369을 나는 다음과 같은 구현
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"/> 

xml 파일에 선형 레이아웃 모두에서 텍스트 뷰를 추가하고 동적 탭 변경 리스너를 사용하여 텍스트 설정 : -

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"); 
    } 
});