Android星级评分条实现评分界面

 更新时间:2022年05月17日 17:26:53   作者:wj778  
这篇文章主要为大家详细介绍了Android星级评分条实现评分界面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现简单评分界面制作的具体代码,供大家参考,具体内容如下

简单评分界面的制作

实现如图界面

1.先布局,创建布局文件,使用相对布局,添加一个编辑框,一个文本框,一个评分条,再加一个按钮。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/etxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="5"
        android:hint="请评价店铺的服务态度与服务质量"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/txt"
        android:layout_below="@id/etxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="店铺评分"

        android:layout_marginTop="20dp"
        android:textSize="20sp"/>
    <RatingBar
        android:id="@+id/ratingbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txt"//使用numStars=""来设置
        android:stepSize="1"//设置每次一颗一颗增加
        android:rating="5"//设置默认五颗星都是亮的
        />
    <Button
        android:id="@+id/btn"
        android:layout_below="@id/ratingbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/txt"
        android:text="发表评价"/>

</RelativeLayout>

接下来在java代码当中实现对按钮监听

package com.example.relativelayout;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class RatingBar_Activity  extends AppCompatActivity {
    private RatingBar ratingBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ratingbar_main);
        ratingBar=findViewById(R.id.ratingbar);
        Button btn=findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float rating=ratingBar.getRating();//获取当前的星数
                Toast.makeText(RatingBar_Activity.this,"你评价了"+rating+"颗星",Toast.LENGTH_LONG).show();
            }
        });

    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

最新评论