android scrollview嵌套listview,Android SeekBar和RatingBar示例教程

 2023-11-19 阅读 30 评论 0

摘要: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應用程序中實現

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應用程序中實現SeekBarRatingBar 。 在轉到實現之前,讓我們定義它們并討論它們的用法。

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.

SeekBarProgressBar的擴展,添加了可拖動的滑塊。 用戶可以觸摸拇指并向左或向右拖動,從而允許用戶更改其所連接的組件的值。 它的用途包括設備亮度控制和音量控制。

Similar to the ProgressBar, it uses two properties that are android:max and android:progress.

ProgressBar相似,它使用android:maxandroid: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" ><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-seekbar-example

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項目

Download Android SeekBar RatingBar Project下載Android SeekBar RatingBar項目

翻譯自: https://www.journaldev.com/9635/android-seekbar-and-ratingbar-example-tutorial

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/183334.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息