自定义Toast和RatingBar的简单用例

时间:2021-07-26 07:38:36

Toast是一个包含用户点击消息。Toast类会帮助你创建和显示这些。Android中的Toast是一种简易的消息提示框。 当视图显示给用户,在应用程序中显示为浮动

向右划动五角星增加

自定义Toast和RatingBar的简单用例自定义Toast和RatingBar的简单用例

单击按钮显示自定义Toast

自定义Toast和RatingBar的简单用例

BaseActivity
package com.shaoxin.myratingbar;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity; /**
* Created by shaoxin on 2016/12/4.
*/ public abstract class BaseActivity extends AppCompatActivity {
public abstract void init();
public abstract void listener();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
listener();
}
}
MainActivity
package com.shaoxin.myratingbar;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends BaseActivity {
private RatingBar ratingbar;
private Button btn; @Override
public void init() {
setContentView(R.layout.activity_main);
ratingbar = (RatingBar) findViewById(R.id.ratingbar);
btn = (Button) findViewById(R.id.btn);
} @Override
public void listener() {
ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Log.v("msg", String.valueOf(rating));
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = new Toast(MainActivity.this);
View tost_view = View.inflate(MainActivity.this, R.layout.toast_view, null);
TextView textView = (TextView) tost_view.findViewById(R.id.txt);
textView.setText(btn.getText().toString());
toast.setView(tost_view);
toast.show();
}
});
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.shaoxin.myratingbar.MainActivity"> <RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.1"
android:isIndicator="false"
android:progressDrawable="@drawable/ratingbar_style" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Toast 自定义"/>
</LinearLayout>

toast_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.shaoxin.myratingbar.MainActivity"> <RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.1"
android:isIndicator="false"
android:progressDrawable="@drawable/ratingbar_style" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Toast 自定义"/>
</LinearLayout>