android學習---SeekBar和RatingBar

來源:互聯網
上載者:User

標籤:des   android   blog   http   io   ar   os   sp   java   

SeekBar 拖動條:拖動條和滾動欄類似,當是拖動條能夠拖動滑塊改變進度

RatingBar 星級評等條:星級評等條與拖動條相似


SeekBar特有的xml屬性

android:thumb    指定一個Arawable對象,作為之定義滑塊



RatingBar特有的xml屬性

android:isIndicator   是否同意使用者改變(true為不同意改動)

android:numStars   共同擁有多少個星級

android:rating   預設的星級

android:stepSize   每次至少改變多少個星級



為了知道改變的進度而做對應的操作,我們須要加入監聽

SeekBar 的監聽 OnSeekBarChangeListener

RatingBar的監聽 OnRatingBarChangeListener


以下我們通過執行個體來熟悉它們

(1)編寫布局檔案   activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/linearLayout"     android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">        <ImageView         android:id="@+id/imgView1"        android:layout_width="120dp"        android:layout_height="100dp"        android:src="@drawable/ic_launcher"/>    <SeekBar         android:id="@+id/seekBar"        android:layout_width="180dp"        android:layout_height="wrap_content"        android:thumb="@drawable/ic_launcher"        android:max="255"        android:progress="255"/>      <ImageView         android:id="@+id/imgView2"        android:layout_width="120dp"        android:layout_height="100dp"        android:src="@drawable/img01"/>      <RatingBar           android:id="@+id/reatingBar"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:max="255"          android:progress="255"          android:rating="5"          android:stepSize="0.5"          android:numStars="5"/>    </LinearLayout>

(2)編寫 MainActivity.java

package com.example.bar;import android.annotation.TargetApi;import android.app.Activity;import android.os.Build;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.widget.ImageView;import android.widget.RatingBar;import android.widget.RatingBar.OnRatingBarChangeListener;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;@TargetApi(Build.VERSION_CODES.HONEYCOMB)public class MainActivity extends Activity {private ImageView imgViewSB = null; //定義ImageViewprivate ImageView imgViewRB = null;private SeekBar seekBar = null;  //定義SeekBarprivate RatingBar ratingBar = null; //定義RatingBar@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//布局組件this.imgViewSB = (ImageView) findViewById(R.id.imgView1);this.imgViewRB = (ImageView) findViewById(R.id.imgView2);this.seekBar = (SeekBar) findViewById(R.id.seekBar);this.ratingBar = (RatingBar) findViewById(R.id.reatingBar);//seekBar設定監聽,改變圖片透明度this.seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {Log.i("onStopTrackingTouch", "停止拖動觸發的方法");}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {Log.i("onStartTrackingTouch", "開始拖動觸發的方法");}/** * seekBar: SeekBar對象 * progress:拖動條的進度 * fromUser:是否使用者手動改變 */@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {imgViewSB.setAlpha(progress);  //設定圖片的透明度Log.i("onProgressChanged", "拖動滑塊位置發生改變時觸發的方法");}});//ratingBar設定監聽,改變圖片透明度this.ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {/** * ratingBar:RatingBar對象 * rating :星級的大小 * fromUser:是否使用者手動改變 */@Overridepublic void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {imgViewRB.setAlpha((int)(rating*255/5));}});}@Overridepublic 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;}}

同意之後,效果例如以下:


android學習---SeekBar和RatingBar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.