Android Tutorial TabHost Tutorial

klaus

Expert
Licensed User
Longtime User
Die TabHost View ist eine sehr wichtige View. Sie erlaubt mehrere Layouts in eine einzige Activity einzufügen.

Der komplette Sourcecode befindet sich am Ende.
Das Englische Originaltutorial.


tabhost_3.png


Vorläufig erlaubt der Designer nicht Views direkt in die TabHost View einzufügen.
Man kann nur die TabHost einfügen und ihr Layout definieren:

tabhost1.png


Es gibt mehrere Möglichkeiten, um TabPages (Tabseiten) hinzuzufügen. Normalerweise ist es empfehlenswert, eine Layout-Datei im Designer für jede Seite (TabPage) zu erstellen und sie dann zu laden.

Der Designer behandelt alle Layout-Datein separat. Es ist Ihre Verantwortung, den Views verschiedene Namen zu geben, es ist nicht erlaubt mehrere Views mit dem gleichen Namen zu erstellen (das ist nur für Views gültig die einen programmgesteuerten Zugriff erfordern).

Das wird mit AddTab oder AddTabWithIcon gemacht.
Beispiel:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    Dim bmp1, bmp2 As Bitmap
    bmp1 = LoadBitmap(File.DirAssets, "ic.png")
    bmp2 = LoadBitmap(File.DirAssets, "ic_selected.png")
    
    TabHost1.AddTabWithIcon ("Name", bmp1, bmp2, "page1") 'load the layout file of each page
    TabHost1.AddTab("Color", "page2") 
    TabHost1.AddTab("Animal", "page3")
End Sub
AddTabWithIcon enthält zwei Bitmaps. Tatsächlich braucht man zwei Icons. Eins, wenn das Tab ausgewählt ist und eine wenn das Tab nicht aktiviert ist. Die Richtlinien empfehlen die Schaffung einer dunklen Version für das ausgewählte Symbol und eine helle Version für das nicht ausgewählte Symbol.

Sie können manuell ein Tab mit der CurrentTab Eigenschaft (property) wählen.

Der komplette Sourcecode befindet sich am Ende.


B4X:
Sub Process_Globals
    
End Sub

Sub Globals
    Dim TabHost1 As TabHost
    Dim txtName, txtAnimal, txtColor As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    Dim bmp1, bmp2 As Bitmap
    bmp1 = LoadBitmap(File.DirAssets, "ic.png")
    bmp2 = LoadBitmap(File.DirAssets, "ic_selected.png")
    
    TabHost1.AddTabWithIcon ("Name", bmp1, bmp2, "page1") 'load the layout file of each page
    TabHost1.AddTab("Color", "page2") 
    TabHost1.AddTab("Animal", "page3")
End Sub
Sub Activity_Pause (Finishing As Boolean)
    
End Sub
Sub Activity_Resume

End Sub
Sub btnNext1_Click
    TabHost1.CurrentTab = 1 'move to next tab
End Sub

Sub btnNext2_Click
    TabHost1.CurrentTab = 2 'move to next tab
End Sub

Sub btnDone_Click
    Dim sb As StringBuilder
    sb.Initialize
    sb.Append("You have entered:").Append(CRLF)
    sb.Append("Name: ").Append(txtName.Text).Append(CRLF)
    sb.Append("Color: ").Append(txtColor.Text).Append(CRLF)
    sb.Append("Animal: ").Append(txtAnimal.Text)
    Msgbox(sb.ToString, "")
End Sub

Sub TabHost1_TabChanged
    Activity.Title = "Current Tab = " & TabHost1.CurrentTab 
End Sub
Sourcecode:
zip.gif
TabHost.zip
 
Last edited:

Metall4You

Member
Licensed User
Longtime User
Tabhost mit Edittext und Wheel

Hallo Klaus

Habe ein Tabhost mit 3 Layouts und Edittextboxes. Jetzt wenn ich zwischen den Tabs wechsle bekommt immer eine Edittextbox den Focus und das Wheel geht auf...

Habe folgenden Code bei der Textbox hinterlegt wie im Beispiel.

Sub txtDatum_FocusChanged (HasFocus As Boolean)
If HasFocus = True Then
Log(DateTime.DateFormat)
whlDate.Show(txtDatum, "11/22/2001")
whlDate.SetBackgroundColor(Colors.Gray)
whlDate.SetWindowLineColor(Colors.Gray)
whlDate.SetWindowColor(Colors.ARGB(96, 192, 192, 224))
End If
End Sub

Wie kann ich den Focus unterdrücken?

Gruss Ramon
 

Beja

Expert
Licensed User
Longtime User
danke, aber leider ich can nicht verstehen.
 

klaus

Expert
Licensed User
Longtime User
Well, you are in the German Tutorials forum.
So the answer is in german !
The TabHost Tutorial in the first post is a german translation of Erel's original tutorial.

Haven't you tried Google translation ?
That's what I use when looking at foreign language forums that I don't understand.

The question was when having several EditText views in a TabHost page and the first one of those EditText views is calling a clsWheel object in the FocusChanged event.
When this page is selected the EditText FocusChanged event is automatically raised showing the clsWheel object directly. The question is how to avoid this.

My suggestion is to use a Label and the Label_Click event instead of the EditText.

Best regards.
 

Beja

Expert
Licensed User
Longtime User
The question was when having several EditText views in a TabHost page and the first one of those EditText views is calling a clsWheel object in the FocusChanged event.
When this page is selected the EditText FocusChanged event is automatically raised showing the clsWheel object directly. The question is how to avoid this.

I think it is easier to go back and read it in German!
(just joking)

Thank you Klaus very much and sorry I didn't notice where I was at the time while browsing the forum.
Also thank you for the generosity to explain the question in English when you didn't have to.
 

klaus

Expert
Licensed User
Longtime User
I think it is easier to go back and read it in German! (just joking)
Of course if you know German.
But anyway, this depends on your knowledge of the given language.
Google translation is a workaround better than nothing.
I made the experience that when translating from italian or spanish to french (my daily spoken lanuage) I didn't really understand the question. Most of the time translating to english was better. Sometimes I also tried German but also not the best.

Best regards.

Deutsche Übersetzung, wir sind hier ja in einer Deutschen Umgebung.
Ich glaube es ist besser zurück zu gehen und es in Deutsch zu lesen! (nur ein Scherz)
Klar, wenn Du Deutsch kennst.
Aber, das hängt von deinen Kenntnissen der gegebenen Sprache ab.
Eine Google Übersetzung ist eine 'Krücke' die besser ist als nichts.
Ich habe die Erfahrung gemacht wenn Ich von Italienisch oder Spanisch nach Französisch (meine tagtägliche Sprache) übersetzte, habe Ich die Frage nicht richtig verstanden. In meisten Fällen war die Übersetzung nach Englisch besser. Manchmal versuche Ich es auch nach Deutsch, auch nicht das Beste.

Beste Grüsse.
 

Beja

Expert
Licensed User
Longtime User
My German is less than perfect.. I went to language school in Dortmund in 1979! (Ausländer Institut).. a friend of mine, the late Peter-Anton von Arnim (Pava) helped me a lot in learning the language, beside some German culture (Goethe,
Schiller, Bettina von Arnim.. now I can recite Goethe Gedichte like Prometheus and others... but that was long time ago.. a book about Pava's life and works will be out soon before the end of this year.
I found near perfect the translation between German and English, and will use it here so I can benefit from the German forum that I (feel) contains rich material.
 
Last edited:

Similar Threads

Top