繁体   English   中英

AlertDialog中的RatingBar:如何以编程方式更改星星数?

[英]RatingBar in a AlertDialog : How to change the number of stars programmatically?

我尝试在AlertDialog中显示RatingBar(通常从Handler调用)。

我的问题是我无法更改星数。 我知道那里有很多答案,但是总是通过xml传递,这是我不想要的。

有人可以帮忙吗?

public void taskRatingPopup() {
    final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
    final RatingBar rating = new RatingBar(this);
    rating.setMax(5);
    rating.setNumStars(5);
    rating.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));


    popDialog.setIcon(android.R.drawable.btn_star_big_on);
    popDialog.setTitle("BlaBla");
    popDialog.setView(rating);

    // Button OK
    popDialog.setPositiveButton(android.R.string.ok,
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {

    writeRatingFile(rating.getProgress());

    //Remove the dialog
    dialog.dismiss();
    }

    })

    // Button Cancel
    .setNegativeButton("Cancel",
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    dialog.cancel();
    }
    });

    popDialog.create();
    popDialog.show();
}

非常感谢您的帮助!

//code block
        public void itemRatingPopup() {
            final AlertDialog.Builder popDialog = new AlertDialog.Builder(
                    mainScreen);
            final RatingBar rating = new RatingBar(mainScreen);
            rating.setNumStars(5);
            rating.setStepSize(0.1f);
            rating.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            LinearLayout parent = new LinearLayout(mainScreen);
            parent.setGravity(Gravity.CENTER);
            parent.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT));
            parent.addView(rating);

            // popDialog.setIcon(android.R.drawable.btn_star_big_on);
            popDialog.setTitle(mainScreen.getResources().getString(
                    R.string.user_ratings));
            popDialog.setView(parent);

            // Button OK
            popDialog.setPositiveButton(R.string.submit,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            String fanid = StorageManager.getInstance()
                                    .getSharedPrefs(mainScreen)
                                    .getString(AppConstants.PREFS_FAN_ID, "0");
                            if (fanid != null) {
                                new SubmitUserRatingAsyncTask("Item", ""
                                        + item.getId(), fanid, rating.getRating())
                                        .execute();
                            }
                            dialog.dismiss();
                        }
                    }).setNegativeButton(R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            popDialog.create();
            popDialog.show();
        }

您的对话框UI不会精确显示5星,因为您已将RatingBar分配为Dialog的根视图,所以它可能会显示任意数目的开始,以供参考,请参见上面的代码:

    you can change rating programatically by below code:
    float myRating=0f;
    rating.setRating(myRating)

用于添加编号 评级栏中的“星级”,只需获取该数字并使用此标签即可。

rb_rate.setNumStars(<no.of stars,integer value>);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM