Android 1.5 Programmation avec TabHost et boutons
-
21-08-2019 - |
Question
Je suis en train d'essayer le SDK Android 1.5 et j'ai vu quelques exemples sur TabHost. Ce que je suis en train de faire est d'utiliser un autre bouton sur chaque onglet pour faire ses tâches.
Qu'est-ce que j'ai essayé utilisait onClickListiner () et onClick (). Je pense que c'est ce que tous les développeurs utilisent, mais je continue à obtenir une exception nulle sur le LogCat chaque fois que le bouton est enfoncé. Aussi j'ai chaque mise en page XML de sorte que j'appelle l'onglet comme: tab.add (... setContent (R.id.firstTabLayout))
firstTabLayout = layout for Button and TextView.
Quelle serait la meilleure façon de faire un bouton / travail TextView correctement sous le TabHost?
La solution
Je ne suis pas tout à fait sûr où votre problème est, mais comment j'ai mis mes activités avant Tabbed
layout.xml
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
Tab.java
public class InitialActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
TabHost th = getTabHost();
th.addTab(th.newTabSpec("tab_1").setIndicator("Tab1").setContent(R.id.tab1));
th.addTab(th.newTabSpec("tab_2").setIndicator("Tab2").setContent(R.id.tab2));
th.addTab(th.newTabSpec("tab_3").setIndicator("Tab3").setContent(R.id.tab3));
}
}
Ensuite, vous pouvez simplement utiliser findViewById () sur les points de vue dans les onglets, ou s'il y a des noms partagés entre eux, vous pouvez le faire findViewById(R.id.tab1).findViewById(R.id.text)