1

我尝试在 AlertDialog 中显示 RatingBar(它经常从处理程序中调用)。

我的问题是我不能改变星星的数量。我知道那里有很多答案,但总是通过我不想要的 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();
}

非常感谢您的帮助!

4

2 回答 2

0

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

rb_rate.setNumStars(<no.of stars,integer value>);
于 2014-12-30T06:45:00.787 回答
0
//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)
于 2013-07-04T10:49:22.460 回答