zoukankan      html  css  js  c++  java
  • 自定义Toast和RatingBar的简单用例

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

    向右划动五角星增加

    单击按钮显示自定义Toast

    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>
  • 相关阅读:
    帝国cms在任意位置调用指定id的栏目名称和链接
    Sublime Text 2中前端必备的常用插件
    sublime text 2代码片段(Snippet)功能的使用
    写好PPT的四大要点
    解码郭台铭语录,50句你应该知道的“郭台铭语录”
    java.sql.SQLSyntaxErrorException: Table 'demo.hibernate_sequence' doesn't exist
    Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.example.demo.domain.DeptInfo
    com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别
    Spring Boot引入Lombok
    Spring Boot(二)jpa操作数据库
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/6171369.html
Copyright © 2011-2022 走看看