Continue to learn about the UI components today, mainly including progressbar, seekbar, imageview, and tabhost ).
1. ProgressBar component
The following is a case study:
First, createProgressBarActivity Class
Case implementation process:
Public class progressbardemo extendsactivity {
Progressbar = NULL;
Inti = 0;
Intprogressbarmax = 0;
Handler handler = new handler ();
@ Override
Publicvoid oncreate (bundlesavedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout.SS);
Findviews ();
}
Private void findviews (){
Progressbar = (progressbar) This. findviewbyid (R. Id.Progressbar2);
Progressbar. setmax (1000 );
Progressbarmax = progressbar. getmax ();
New thread (newrunnable (){
Public void run (){
While (I <progressbarMax ){
I = doWork ();
Handler. post (newRunnable (){
Public void run (){
Progressbar. setProgress (I );
}
});
Try {
Thread.Sleep(50 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
}). Start ();
}
Public int doWork (){
Return ++ I;
}
}
Code demonstration in the layout file progressbar. xml
<? Xml version ="1.0"Encoding ="UTF-8"?>
<LinearLayoutxmlns: android =Http://schemas.android.com/apk/res/android"
Android: layout_width ="Fill_parent"
Android: layout_height ="Fill_parent"
Android: orientation ="Vertical">
<TextView
Android: layout_width ="Fill_parent"
Android: layout_height ="Wrap_content"
Android: text ="Progress bar demonstration"/>
<ProgressBar
Android: layout_width ="Wrap_content"
Android: layout_height ="Wrap_content"
Android: max ="1000"
Android: progress ="100"
Android: id ="@ + Id/progressbar1"
/>
<ProgressBar
Style ="@ Android: style/Widget. ProgressBar. Horizontal"
Android: layout_margintop ="30dp"
Android: layout_width ="Fill_parent"
Android: layout_height ="Wrap_content"
Android: max ="1000"
Android: Progress ="100"
Android: secondaryprogress ="300"
Android: Id ="@ + Id/progressbar2"
/>
</Linearlayout>
Configuration file:
<Activity
Android: Label ="@ String/app_name"
Android: Name =".Progressbardemo">
<Intent-filter>
<Action Android: Name ="Android. intent. action. MAIN"/>
<Category Android: Name ="Android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
The result is returned:
2. TabHost (switch component)
Internal class:
1. Interface tabhost. ontabchangelistener
Interface defines the callback function called when the tab is changed.
2. interfaceTabHost. TabContentFactory
Tab content is generated when a tab is selected
3. class TabHost. TabSpec
A separate tab. Each option card has a tab indicator, content, and tag to facilitate recording.
Method:
Public voidaddTab (TabHost. TabSpec tabSpec)
Add a new tab
Parameters
TabSpec -- specify how to create the indicator and content.
The following is a case study:
First, create an Activity class named TabHost.
Code reference:
Public classTabHostDemoExtendsTabActivity {
TabHost tabHost =Null;
@ Override
Protected voidOnCreate (Bundle savedInstanceState ){
Super. OnCreate (savedInstanceState );
TabHost =This. GetTabHost ();
LayoutInflaterinflater = LayoutInflater.From(This);
// Obtain it to tabhost_layout.
Inflater. inflate (R. layout.Tabhost_layout, TabHost. getTabContentView (),True);
TabHost. addTab (tabHost. newTabSpec ("tab1"). setIndicator ("Switch tag"). setContent (R. id.Tab1));
TabHost. addTab (tabHost. newTabSpec ("tab2"). setIndicator ("SeekBardemo ").
SetContent (NewIntent (This, SeekBarDemo.Class)));
// SetIndicator indicates the label name tab1 tab2 tab3 is his ID value
TabHost. addTab (tabHost. newTabSpec ("tab3"). setIndicator ("ImageViewDemo ").
Setcontent (NewIntent (This, Imageviewdemo.Class)));
Findviews ();
}
Private voidFindviews (){
Button BTN = (button)This. Findviewbyid (R. Id.Button);
// Listener
BTN. setonclicklistener (NewView. onclicklistener (){
@ Override
Public voidOnclick (view v ){
// Tabhost. setcurrenttab (1 );
Tabhost. setcurrenttabbytag ("tab2 ");
}
});
}
}
Code in tabhost. XML in the layout file:
<Tabhostxmlns: Android =Http://schemas.android.com/apk/res/android"
Android: layout_width ="Fill_parent"
Android: layout_height ="Fill_parent">
<Linearlayout
Android: Id ="@ + Id/tab1"
Android: layout_width ="Match_parent"
Android: layout_height ="Match_parent"
Android: Orientation ="Vertical">
<Button
Android: id ="@ + Id/button"
Android: layout_width ="Match_parent"
Android: layout_height ="Wrap_content"
Android: text ="Switch to tab2"/>
<ImageView
Android: layout_width ="Wrap_content"
Android: layout_height ="Wrap_content"
Android: src ="@ Drawable/pig"
Android: scaleType ="FitCenter"
Android: layout_marginTop ="20dp"
/>
</LinearLayout>
</TabHost>
The configuration file is as follows:
<Application
Android: icon ="@ Drawable/ic_launcher"
Android: label ="@ String/app_name">
<Activity
Android: label ="@ String/app_name"
Android: name =". HostDemo">
<Intent-filter>
<Action android: name ="Android. intent. action. MAIN"/>
<Category android: name ="Android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Activity
Android: Name =". SeekBarDemo">
</Activity>
<Activity
Android: Name =". ImageViewDemo">
</Activity>
</Application>
Effect
3. SeekBar component
The following is a case study:
First, createSeekBarActivity Class
Case implementation process:
Public classSeekBarDemoExtendsActivity
ImplementsOnSeekBarChangeListener {
SeekBar seekbar =Null;
@ Override
Protected voidOnCreate (Bundle savedInstanceState ){
Super. OnCreate (savedInstanceState );
This. SetContentView (R. layout.Seekbar_layout);
FindViews ();
}
Private voidFindViews (){
Seekbar = (SeekBar)This. FindViewById (R. id.Seekbar);
// Register the listener
Seekbar. setOnSeekBarChangeListener (This);
}
@ Override // method of listening
Public voidOnProgressChanged (SeekBar seekBar,
IntProgress,
BooleanFromUser ){
Log.D("TAG", "changed:" + String.ValueOf(SeekBar. getProgress ()));
}
@ Override
Public voidOnStartTrackingTouch (SeekBar seekBar ){
Log.D("Tag", "Start:" + String.ValueOf(Seekbar. getprogress ()));
}
@ Override
Public voidOnstoptrackingtouch (seekbar ){
Log.D("Tag", "Stop:" + String.ValueOf(Seekbar. getprogress ()));
}
}
Layout FileSeekbarCode demonstration in. xml
<Linearlayoutxmlns: Android =Http://schemas.android.com/apk/res/android"
Android: layout_width ="Match_parent"
Android: layout_height ="Match_parent"
Android: Orientation ="Vertical">
<Seekbar
Android: layout_width ="Fill_parent"
Android: layout_height ="Wrap_content"
Android: max ="1000"
Android: id ="@ + Id/seekbar"/>
</LinearLayout>
Configuration file:
<Activity
Android: label ="@ String/app_name"
Android: name =".SeekBarDemo">
<Intent-filter>
<Action android: name ="Android. intent. action. MAIN"/>
<Category android: name ="Android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
Effect