• 自定义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>
  • 相关阅读:
    获取路径API:GetCurrentDirectory、GetModuleFileName
    任务栏显示程序详解
    MSXML读取XML文件
    解决VS断点无效问题
    非console程序显示DOS窗口
    Windows系统版本宏_WIN32_WINNT对应值
    sql2005 reporting service,我总算找到一个完全程序化绑定报表(ado.net dataset 绑定reprot)的方案,谁能再给我些其他建议呢? Carlwave
    在ASP.net中保存/取出图片入/从SQL数据库(可用于上传图片) Carlwave
    谁能谈谈国外软件行业的实际情况么?(全美“50大好差事” 软件工程师排名第一) Carlwave
    转:微软今年必做三大事(from csdn.net) Carlwave
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/6171369.html
Copyright © 2020-2023  润新知