Android RatingBar实现五星好评

属性

isIndicatorRatingBar   是否为指示器,为true时,用户将无法交互操作,默认为false。

numStars                      显示的星型数量,必须是一个整形值,像“50”,虽然可以设置很大,但一般都是5-10个星星即可。

rating                            设置默认的评分。

stepSize                        评分每次增加的值。建议大于0小于等于1之间最合适。

样式

其中内置了三个样式:

style="?attr/ratingBarStyle":默认样式

style="?android:attr/ratingBarStyleSmall":小样式

style="?android:attr/ratingBarStyleIndicator":指示器样式

 dialog弹框显示好评操作效果图

 Android RatingBar实现五星好评_第1张图片

布局:




    

    

        


        

            

        


    

    

        


        

            
        


    

    

        


        

            
        


    

    

    

        


    


    

        

        

    

drawable ---->  shape_while_radius_10dp_style.xml



    
    

drawable -----> shape_color_remakes_label_style.xml




    
    

    
    

values ------> styles.xml

  RatingBar颜色:

    
    

 Dialog主题:

    

drawable -----> shape_247eff_30_stroke_style.xml




    
    


drawable -----> shape_247eff_radius_30dp_style.xml



    

    
    

    
    


CenterDialogView:
public class CenterDialogView {


    private static CenterDialogView mInstance;
    private Context mContext;
    private Dialog mDialog;
    private static final String TAG = "CenterDialogViewTag";
    private long exitTime = 0;


    private CenterDialogView(Context context) {
        this.mContext = context;
        createDialog(context);
    }

    private void createDialog(Context context) {
        //1、使用Dialog、设置style
        mDialog = new Dialog(context, R.style.CenterDialogTheme);
        mDialog.setCancelable(true);

        //设置setCancelable为false之后,物理返回按键不能取消弹框。需要设置该监听,以达到按返回键取消弹框的目的
        mDialog.setOnKeyListener((dialog, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                if ((System.currentTimeMillis() - exitTime) > 2000) {
                    exitTime = System.currentTimeMillis();
                } else {
                    mDialog.cancel();
                }
                return true;
            }

            return false;
        });
        
    }

    public static CenterDialogView getInstance(Context context) {
         
        if (mInstance == null) {
            synchronized (CenterDialogView.class) {
                if (mInstance == null) {
                    mInstance = new CenterDialogView(context);
                }
            }
        }
        return mInstance;
    }


    public Dialog loadDialogView(View view, float widthPx, float heightPx) {

        //2、设置布局
        mDialog.setContentView(view);

        Window window = mDialog.getWindow();

        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        WindowManager.LayoutParams layoutParams = mDialog.getWindow().getAttributes();
        layoutParams.width = (int) widthPx;
        layoutParams.height = (int) heightPx;
        if (heightPx == 0) {
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        }

        mDialog.getWindow().setAttributes(layoutParams);
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        return mDialog;

    }

    public void showDialog() {
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     *
     * @param dpValue 尺寸dip
     * @return 像素值
     */
    private int dp2px(float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public void cancelDialog() {
        if (mDialog != null) {
            mDialog.cancel();
        }
    }


    /**
     * 评论弹框
     *
     * @return
     */
    public View CommentDriverDialog(OnCommentDriverListener onCommentDriverListener) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.comment_task_dialog, null, false);

        RatingBar mRtbAccident = view.findViewById(R.id.mRtbAccident);
        RatingBar mRtbRegulations = view.findViewById(R.id.mRtbRegulations);
        RatingBar mRtbDiscipline = view.findViewById(R.id.mRtbDiscipline);
        EditText mEtRemarks = view.findViewById(R.id.mEtRemarks);
        TextView mTvCancelDialog = view.findViewById(R.id.mTvCancelDialog);
        TextView mTvConfirmDialog = view.findViewById(R.id.mTvConfirmDialog);
        mRtbAccident.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                Log.i("onRatingChangedTAG", "rating: " + rating + ", fromUser:" + fromUser);
            }
        });
        view.findViewById(R.id.mTvCancelDialog).setOnClickListener(v -> {
            if (mDialog != null) mDialog.cancel();
        });
        view.findViewById(R.id.mTvConfirmDialog).setOnClickListener(v -> {
            if (onCommentDriverListener != null) onCommentDriverListener.onClick();
        });

        return view;
    }

    public interface OnCommentDriverListener {
        void onClick();
    }


    public void destroyView() {
        Log.d(TAG, "destroyView: ");
        if (mDialog != null) {
            mDialog.cancel();
            mDialog = null;
        }
        if (mInstance != null) {
            mInstance = null;
        }
    }

}

 

你可能感兴趣的:(Android,android,五星好评控件)