标签:utf-8 void auto 一个 klist context mamicode app android
注:
博客:
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();; } }); } }
标签:utf-8 void auto 一个 klist context mamicode app android
原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/12158731.html