編輯:關於 Android 程式設計
RatingBar 是專門拿來做評分、評級的控制項,本質上是 SeekBar 的延伸,預設以星號呈現。使用上只要掛一個 OnRatingBarChangeListener 即可。
public class MainActivity extends Activity {
private RatingBar ratingBar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ratingBar = (RatingBar) findViewById(R.id.myRatingBar);
ratingBar.setOnRatingBarChangeListener(new setRatingBarListener());
}
class setRatingBarListener implements RatingBar.OnRatingBarChangeListener {
// 等級改變時呼叫
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
System.out.println("RatingBar --->" + rating);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<!-- numStars 表示有幾顆星;stepSize 表示每次移動前進幾顆星。
寬度建議用 wrap_content,否則 numStars 只會被當成評分刻度,
而不是實際顯示的星數 -->
<RatingBar
android:id="@+id/myRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/myText"
android:numStars="4"
android:stepSize="0.5" />
</RelativeLayout>
android:numStars 決定星數,設為 0 會變成連續的長條狀評分條。android:stepSize 決定每次變動的幅度,寫 0.5 就能做出半星效果,寫 1 則只能整顆。android:rating 可以在布局中直接指定初始值。
寬度那個細節值得留意:如果 layout_width 寫成 fill_parent 或明確數值,numStars 的意義會變成「在這個寬度內切幾等分」,星星的實際數量就不受控了。另外 RatingBar 預設是「不可取消」的——使用者點同一顆星不會回到 0,若需要這個行為,得自行在 onRatingChanged 中判斷並呼叫 setRating(0)。樣式方面也可以透過 style="?android:attr/ratingBarStyleSmall" 之類的內建樣式換外觀,或用 progressDrawable 自訂星圖。
深入 Android 消息機制:Handler 到底是怎麼運轉的
Handler 是什麼 Handler 是 Android 提供的一套消息處理機制,同時也是我們更新 UI 的主要通道。它既能發送消息,也能處理消息。 為什麼非用它不可
Android Notes:深入探索 Activity 的生命周期
Activity 作為四大組件之一,也是與用戶交互最多的組件。為了實現更好的交互效果,了解 Activity 的生命周期、並在每個階段正確分配應完成的工作就顯得十分必要
Android N 開發者功能總覽:從多視窗到直接啟動
值得關注的新能力一覽 多視窗支援 通知體驗升級 JIT/AOT 編譯 更快的應用安裝流程 行動打盹(Doze on the Go) 背景執行最佳化 Data Save
Android 開發工具實戰:Eclipse/ADT 必備插件與 Android Studio 踩坑筆記
Eclipse/ADT:那個曾經的經典陣營 ADT 是 Google 在 Eclipse 之上打包出來的 Android 開發環境,最後一個版本停在 2014 年 7 月