나는 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;
}
}
n 개의 탭이 있고 탭 수가 동적으로 변경 될 때 어떻게 동일한 작업을 수행 할 수 있는지 설명 할 수 있습니까? ally –
'int numTabs = mTabHost.getTabWidget(). getTabCount();'과 같은 것을 할 수 있으며 for 루프를 사용하여 각 탭을 가져 와서 제목 텍스트를 찾고 긴 클릭 리스너를 설정합니다. –