Click the button in Android to get the score of star rating bar

Keywords: Android Programming xml encoding

scene

Effect

 

 

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
Pay attention to the public address
Domineering procedural ape
Get programming related ebooks, tutorials and free downloads.

Realization

Change the layout to LinearLayout and set it to vertical layout through Android: orientation = "vertical" > then add a RatingBar, and use the

android:rating="5"

 

Set the number of stars to 5

Then add a Button to add Id to them.

<?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="Publication evaluation"
       />

</LinearLayout>

 

Then go to Activity, get RatingBar and button through Id, get star number in button click event, and prompt.

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,"Your score is:"+rating+"branch",Toast.LENGTH_SHORT).show();;
            }
        });
    }
}

Posted by cyberplasma on Mon, 06 Jan 2020 10:37:51 -0800