8-Android基本組件之常用widget組件介紹(日期選擇器,時間選擇器,滾動視圖,進度條,拖動條,評分組件)_第1頁
8-Android基本組件之常用widget組件介紹(日期選擇器,時間選擇器,滾動視圖,進度條,拖動條,評分組件)_第2頁
8-Android基本組件之常用widget組件介紹(日期選擇器,時間選擇器,滾動視圖,進度條,拖動條,評分組件)_第3頁
8-Android基本組件之常用widget組件介紹(日期選擇器,時間選擇器,滾動視圖,進度條,拖動條,評分組件)_第4頁
8-Android基本組件之常用widget組件介紹(日期選擇器,時間選擇器,滾動視圖,進度條,拖動條,評分組件)_第5頁
已閱讀5頁,還剩21頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

8Android基本組件之常用widget組件介紹(日期選擇器,時間選擇器,滾動視圖,進度條,拖動條,評分組件)學習目標★掌握Android中常用的widget組件★應用widget組件制作自己的UI應用★日期選擇器(DatePicker)介紹與應用★時間選擇器(TimePicker)介紹與應用★滾動視圖(ScrollView)介紹與應用★進度條(ProgressBar)介紹與應用★拖動條(SeekBar)介紹與應用★評分組件(RatingBar)介紹與應用日期選擇器(DatePicker)介紹與應用

DatePicker是一個日期選擇的組件,可以提供快速選擇日期的方式。這里同樣先在main.xml文件中添加一個按鈕,用以打開演示DatePicker組件的界面,實現代碼如下:

<Button

android:id="@+id/date_picker_button"

android:layout_width="wrap_content"android:layout_height="wrap_content"

android:text=“哥顯示的是日期選擇器"/>設定完按鈕后,編寫響應代碼:日期選擇器(DatePicker)介紹與應用Buttondate_picker_button=(Button)findViewById(R.id.date_picker_button);date_picker_button.setOnClickListener(date_picker_button_listener);……privateButton.OnClickListenerdate_picker_button_listener=newButton.OnClickListener(){public

voidonClick(Viewv){Intentintent=newIntent();intent.setClass(MainActivity.this,DatePickerActivity.class);startActivity(intent);}};在單擊按鈕后,會啟動一個新的DatePickerActivity,所以這里我們應該創(chuàng)建一個新的DatePickerActivity.java文件。實現代碼如下:public

voidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setTitle("哥就是數日子的日期選擇器...");setContentView(R.layout.date_picker);DatePickerdp=(DatePicker)this.findViewById(R.id.date_picker);dp.init(2010,5,19,null);在上面代碼我們可以看到,調用了一個date_picker.xml的文件,所以,我們要設計一下顯示的多選框的樣式,對應設計代碼如下:代碼見mouse_widget中的date_picker.xml文件。接著在AndroidManifest.xml中添加如下代碼:<activity

android:name="DatePickerActivity"></activity>日期選擇器(DatePicker)介紹與應用

日期選擇器(DatePicker)

日期選擇器(DatePicker)演示界面時間選擇器(TimePicker)介紹與應用TimePicker是時間選擇組件,可以提供快速選擇和調整時間的方式。這里現在main.xml中添加一個按鈕,單擊按鈕會啟動這個TimePickerActivity,添加Button的代碼如下:

<Button

android:id="@+id/time_picker_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=“哥顯示的是時間選擇器"

/>

定義一個id為“time_picker_button”的按鈕,并設定其寬度和高度都是和內容自適應,并設定其顯示文字為“哥顯示的是時間選擇器”。

其對應的響應代碼如下:Buttontime_picker_button=(Button)findViewById(R.id.time_picker_button);time_picker_button.setOnClickListener(time_picker_button_listener);……privateButton.OnClickListenertime_picker_button_listener=newButton.OnClickListener(){public

voidonClick(Viewv){Intentintent=newIntent();intent.setClass(MainActivity.this,TimePickerActivity.class);startActivity(intent);}};這里創(chuàng)建了一個新的Activity,名字叫TimePickerActivity。同時創(chuàng)建time_picker.xml的界面UI文件,代碼見time_picker.xml下面我們來看下TimePickerActivity.java中的代碼:時間選擇器(TimePicker)介紹與應用public

voidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setTitle("哥是準時的時間選擇器...");setContentView(R.layout.time_picker);TimePickertp=(TimePicker)this.findViewById(R.id.time_picker);tp.setIs24HourView(true);}接著在AndroidManifest.xml中添加如下代碼:<activity

android:name="TimePickerActivity"></activity>下面是項目效果圖:時間選擇器(TimePicker)介紹與應用時間選擇器(TimePicker)時間選擇器(TimePicker)介紹與應用滾動視圖(ScrollView)介紹與應用這里每演示一個組件就會在主界面上增加一個按鈕,這時候,按鈕已經差不多滿屏了。所以這里我們需要一個組件ScrollView。

ScrollView的功能主要就是將一個屏幕顯示不了的內容,通過滾動顯示出來,使用這個組件也比較直觀,直接在LinearLayout外面再增加ScrollView組件申明即可,實現代碼如下:<ScrollView

xmlns:android=""

android:layout_width="fill_parent"

android:layout_height="wrap_content"><LinearLayout>……</LinearLayout></ScrollView>

這里將ScrollView套在LinearLayout外面,使得當LinearLayout的內容超過一個屏幕的時候可以滾動瀏覽。效果如下:添加滾動條前添加滾動條后滾動視圖(ScrollView)介紹與應用進度條(ProgressBar)介紹與應用

ProgressBar是個非常有用的組件,其最直觀的感覺就是進度條顯示,但是在Android中進度條有很多種,這里選擇最常見的兩種來說明。首先在main.xml中增加一個按鈕,實現代碼如下:<Button

android:id="@+id/progress_bar_button"

android:layout_width="wrap_content"android:layout_height="wrap_content"

android:text="哥顯示的是進度條"

/>然后添加其響應代碼:Buttonprogress_bar_button=(Button)findViewById(R.gress_bar_button);progress_bar_button.setOnClickListener(progress_bar_button_listener);privateButton.OnClickListenerprogress_bar_button_listener=newButton.OnClickListener(){public

voidonClick(Viewv){Intentintent=newIntent();intent.setClass(MainActivity.this,ProgressBarActivity.class);startActivity(intent);}};

然后創(chuàng)建ProgressBarActivity.java文件,并設定其模板為progress_bar.xml,在progress_bar.xml文件中添加兩種表現顯示的進度條組件,代碼實現如下:進度條(ProgressBar)介紹與應用<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="圓形進度條"

/>

<ProgressBar

android:id="@+id/progress_bar"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/><TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="水平進度條"

/>

<ProgressBar

android:id="@+id/progress_horizontal"

style="?android:attr/progressBarStyleHorizontal"

android:layout_width="200dip"

android:layout_height="wrap_content"

android:max="100"

android:progress="50"

android:secondaryProgress="75"

/>進度條(ProgressBar)介紹與應用進度條(ProgressBar)界面展示ProgressBar顯示效果SeekBar組件和水平的ProgressBar組件功能有些相似,不過其不同點在于SeekBar可以被拖動。首先在main.xml中放置一個顯示SeekBar的按鈕,XML文件代碼如下:<Button

android:id="@+id/seek_bar_button"

android:layout_width="wrap_content"android:layout_height="wrap_content"

android:text="哥顯示的是拖動條"

/>然后添加其響應代碼:拖動條(SeekBar)介紹與應用Buttonseek_bar_button=(Button)findViewById(R.id.seek_bar_button);seek_bar_button.setOnClickListener(seek_bar_button_listener);privateButton.OnClickListenerseek_bar_button_listener=newButton.OnClickListener(){public

voidonClick(Viewv){Intentintent=newIntent();intent.setClass(MainActivity.this,SeekBarActivity.class);startActivity(intent);}};跳轉到SeekBarActivity后,創(chuàng)建一個SeekBarActivity.java的文件,響應Seek_bar.xml里的UI代碼,seek_bar.xml中的代碼如下:拖動條(SeekBar)介紹與應用<TextViewandroid:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="拖動條"/>

<SeekBar

android:id="@+id/seek"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:max="100"

android:thumb="@drawable/seeker"

android:progress="50"/>其在SeekBarActivity.java中的響應代碼如下:拖動條(SeekBar)介紹與應用public

voidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setTitle("哥是拖來拉去的拖動條...");setContentView(R.layout.seek_bar);}接著在AndroidManifest.xml中添加如下代碼:<activity

android:name="SeekBarActivity"></activity>演示結果如下圖:

SeekBar演示效果拖動條(SeekBar)介紹與應用在讓用戶參與評分的時候,用RatingBar組件實現非常方便,第一方便用戶輸入,第二直觀,首先在main.xml中添加一個按鈕文件代碼如下:<Button

android:id="@+id/rating_bar_button"

android:layout_width="wrap_content"android:layout_height="wrap_content"

android:text=“哥顯示的是評分組件"

/>然后添加其響應代碼:評分組件(RatingBar)介紹與應用Buttonrating_bar_button=(Button)findViewById(R.id.rating_bar_button);rating_bar_button.setOnClickListener(rating_bar_button_listener);privateButton.OnClickListenerrating_bar_button_listener=newButton.OnClickListener(){public

voidonClick(Viewv){Intentintent=newIntent();intent.setClass(MainActivity.this,RatingBarActivity.class);startActivity(intent);}};跳轉到RatingBarActivity后,創(chuàng)建一個RatingBarActivity.java的文件,響應rating_bar.xml里的UI代碼,rating_bar.xml中的代碼如下:評分組件(RatingBar)介紹與應用<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="請評分吧~"/>

<RatingBar

android:id="@+id/rating_bar"

android:layout_width="w

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論