In this tutorial we’ll implement a SeekBar and a RatingBar in our android application. Before we jump onto the implementation let’s define them and discuss their usage.
在本教程中,我们将在我们的android应用程序中实现SeekBar和RatingBar 。 在转到实现之前,让我们定义它们并讨论它们的用法。
Android SeekBar (Android SeekBar)
A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right thereby allowing user to change the values of the components it’s attached to. Its usages includes device brightness control and volume control.
SeekBar是ProgressBar的扩展,添加了可拖动的滑块。 用户可以触摸拇指并向左或向右拖动,从而允许用户更改其所连接的组件的值。 它的用途包括设备亮度控制和音量控制。
Similar to the ProgressBar
, it uses two properties that are android:max and android:progress.
与ProgressBar
相似,它使用android:max和android:progress这两个属性。
The SeekBar.OnSeekBarChangeListener interface provides methods to perform event handling callbacks for SeekBar. The methods include:
SeekBar.OnSeekBarChangeListener接口提供了为SeekBar执行事件处理回调的方法。 这些方法包括:
- onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) : It’s used to notify any change in the level of SeekBar. onProgressChanged(SeekBar seekBar,int progress,boolean fromUser) :用于通知SeekBar级别的任何更改。
- onStartTrackingTouch(SeekBar seekBar) : It’s used to notify that the user has started a touch gesture. onStartTrackingTouch(SeekBar seekBar) :用于通知用户已开始触摸手势。
- onStopTrackingTouch(SeekBar seekBar) : It’s used to notify that the user has finished a touch gesture onStopTrackingTouch(SeekBar seekBar) :用于通知用户已完成触摸手势
Android RatingBar (Android RatingBar)
Android RatingBar is used to get the rating from the user. The Rating is returned as a floating-point number.
Android RatingBar用于获取用户的评分。 评级以浮点数形式返回。
Following are the important attributes of a RatingBar
.
以下是RatingBar
的重要属性。
- android:numStars : The number of stars to show in the RatingBar android:numStars :在RatingBar中显示的星数
- android:stepSize : The step size of the rating. A size of 0.5 implies half ratings can be set (such as 3.5) android:stepSize :评分的步长。 大小为0.5表示可以设置一半的额定值(例如3.5)
- android:isIndicator : Whether this rating bar is an indicator that indicates the total number of ratings and is non-changeable by the user android:isIndicator :此评分栏是否为指示评分总数的指示器,并且用户无法更改
- style=”?android:attr/ratingBarStyleSmall” : Creates small indicator RatingBar style style =”?android:attr / ratingBarStyleSmall” :创建小指示器RatingBar样式
The getRating() method of RatingBar returns the rating number.
OnRatingBarChangeListener interface is implemented and the following method needs to be overridden:
RatingBar的getRating()方法返回评分编号。
实现了OnRatingBarChangeListener接口,并且需要重写以下方法:
onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser)
In this tutorial we’ll display a SeekBar along with a TextView to show the corresponding value from the SeekBar at any given time and a RatingBar with the rating displayed in a toast.
在本教程中,我们将显示一个SeekBar和一个TextView,以显示在任何给定时间的SeekBar中的相应值,以及一个RatingBar,其等级以吐司显示。
项目结构 (Project Structure)
码 (Code)
The activity_main.xml
layout contains a SeekBar, RatingBar and a TextView as shown in the xml code below:
activity_main.xml
布局包含一个SeekBar,RatingBar和一个TextView,如下面的xml代码所示:
<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"
tools:context=".MainActivity" >
<SeekBar
android:id="@+id/seekBar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:max="10"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/seekBar"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin" />
<RatingBar android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.5"
android:layout_centerVertical="true"
android:layout_alignRight="@+id/seekBar"
android:layout_alignEnd="@+id/seekBar" />
</RelativeLayout>
The MainActivity.java
is given below:
MainActivity.java
如下所示:
package com.journaldev.seekbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements RatingBar.OnRatingBarChangeListener {
private SeekBar seekBar;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
textView = (TextView) findViewById(R.id.textView);
final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
ratingbar.setOnRatingBarChangeListener(this);
// Initialize the textview with '0'
textView.setText(seekBar.getProgress() + "/" + seekBar.getMax());
seekBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
int progress = 0;
@Override
public void onProgressChanged(SeekBar seekBar,
int progressValue, boolean fromUser) {
progress = progressValue;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Display the value in textview
textView.setText(progress + "/" + seekBar.getMax());
}
});
}
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
Toast.makeText(MainActivity.this, "New Rating: " + rating,
Toast.LENGTH_SHORT).show();
}
}
The method onStartTrackingTouch()
is used to perform any task when the user starts dragging the SeekBar. In our case it’s empty. A Toast represents the new rating value obtained from the RatingBar.
当用户开始拖动SeekBar时,方法onStartTrackingTouch()
用于执行任何任务。 在我们的情况下,它是空的。 Toast表示从RatingBar获得的新评级值。
Below is our application running in android emulator.
This brings an end to this tutorial. You can download the final Android SeekBar and RatingBar Project from the link given below.
以下是我们在android模拟器中运行的应用程序。
本教程到此结束。 您可以从下面给出的链接下载最终的Android SeekBar和RatingBar项目 。
翻译自: https://www.journaldev.com/9635/android-seekbar-and-ratingbar-example-tutorial