Android中点击按钮获取星级评分条的评分

Android中点击按钮获取星级评分条的评分,第1张

概述场景效果  注:博客:https://blog.csdn.net/badao_liumang_qizhi关注公众号霸道的程序猿获取编程相关电子书、教程推送与免费下载。实现将布局改为LinearLayout,并通过android:orientation="vertical">设置为垂直布局,然后添加一个RatingBar,并通过android:rating="5" 场景效果

 

 

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

将布局改为linearLayout,并通过androID:orIEntation="vertical">设置为垂直布局,然后添加一个ratingbar,并通过

androID:rating="5"

 

设置其星数为5

然后再添加一个button,分别给他们添加ID。

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical"    tools:context=".ratingBaractivity">    <ratingbar        androID:ID="@+ID/ratingbar"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:rating="5"       />    <button        androID:ID="@+ID/btn"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:background="#FF5000"        androID:text="发表评价"       /></linearLayout>

 

然后来到Activity,通过ID获取ratingbar和button,在button的点击事件中,获取星级数,并提示。

packagecom.badao.relativelayouttest;importandroIDx.appcompat.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.ratingbar;importandroID.Widget.Toast;public class ratingBaractivity extends AppCompatActivity {    private ratingbarratingbar;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_rating_bar);        ratingbar = (ratingbar) findVIEwByID(R.ID.ratingbar);        button button = (button) findVIEwByID(R.ID.btn);        button.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                float rating = ratingbar.getrating();                Toast.makeText(ratingBaractivity.this,"你的评分为:"+rating+"分",Toast.LENGTH_SHORT).show();;            }        });    }}
总结

以上是内存溢出为你收集整理的Android中点击按钮获取星级评分条的评分全部内容,希望文章能够帮你解决Android中点击按钮获取星级评分条的评分所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1067264.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存