020 ProgressBar(进度条)、SeekBar(拖动条)与星级评分条(RatingBar)

1.ProgressBar(进度条)

(1)介绍

(2)常用属性

(3)xml代码

<ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:max="100"
        android:progress="90" />

2.Seeker(拖动条)

(1)介绍

(2)属性介绍

(3)xml布局文件

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/img1" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

3.RatingBar(星级评分条)

(1)介绍

(2)属性

(3)xml布局

<TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="店铺评分" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5" />

    <Button
        android:id="@+id/button"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交" />

4.java后台

package com.lucky.test24;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    SeekBar seekBar;
    ProgressBar progressBar;
    EditText editText;
    RatingBar ratingBar;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=findViewById(R.id.imageView);
        seekBar=findViewById(R.id.seekBar);
        progressBar=findViewById(R.id.progressBar2);
        editText=findViewById(R.id.editText);
        ratingBar=findViewById(R.id.ratingBar);
        button=findViewById(R.id.button);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            //状态发生改变时,触发的方法
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                progressBar.setProgress(progress);  //设置进度条的进度
                imageView.setAlpha(progress);     //设置图片的亮度
            }

            //刚开始拖动时触发的方法
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            //拖动结束后触发的方法
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1="";
                String str2="";
                str1=str1+ratingBar.getRating(); //获取用户评分
                str2="您的评价为:"+editText.getText().toString()+"
您的评分为:"+str1;
                Toast.makeText(MainActivity.this,str2,Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 5.效果图

原文地址:https://www.cnblogs.com/luckyplj/p/10504639.html