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 scrollview嵌套listview, 在本教程中,我們將在我們的android應用程序中實現SeekBar和RatingBar 。 在轉到實現之前,讓我們定義它們并討論它們的用法。
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執行事件處理回調的方法。 這些方法包括:
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
的重要屬性。
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,其等級以吐司顯示。
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" ><SeekBarandroid: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"/><TextViewandroid: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;@Overrideprotected 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;@Overridepublic void onProgressChanged(SeekBar seekBar,int progressValue, boolean fromUser) {progress = progressValue;}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// Display the value in textviewtextView.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.
以下是我們在android模擬器中運行的應用程序。
本教程到此結束。 您可以從下面給出的鏈接下載最終的Android SeekBar和RatingBar項目 。
翻譯自: https://www.journaldev.com/9635/android-seekbar-and-ratingbar-example-tutorial
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态