android - 我试图在对话框中添加评级栏,但我在星数方面遇到了一些问题

标签 android dialog

我尝试添加有星级的评分栏, 我想让价格有 4 颗星,但在我的代码中我得到超过 7 颗星 当我尝试点击一颗星然后出现双星时..(例如:我点击星 1 然后显示选中的 2 颗星)..

我的例子:

my Example:

我的代码:

public class test extends AppCompatActivity {


   TextView textView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        textView =(TextView)findViewById(R.id.textView1);

        // Button1
        final Button btn1 = (Button) findViewById(R.id.btnAddHotelRate);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ShowDialog();
            }
        });

    }



    public void ShowDialog()
    {
        final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
        final RatingBar rating = new RatingBar(this);
        rating.setMax(4);

        popDialog.setIcon(android.R.drawable.btn_star_big_on);
        popDialog.setTitle("Add Rating: ");
        popDialog.setView(rating);

        // Button OK
        popDialog.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        textView.setText(String.valueOf(rating.getProgress()));
                        dialog.dismiss();
                    }

                })

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

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

    }

}

最佳答案

Issue is because RatingBar width matches the parent view and it creates stars irrespective of number of stars, RatingBar should have width as wrap_content respect to the parent.

尝试将 RatingBar 放在 LinearLayout 中,并将 LinearLayout 添加到弹出对话框中。

public void ShowDialog()
{
    final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);

    LinearLayout linearLayout = new LinearLayout(this);
    final RatingBar rating = new RatingBar(this);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );
    rating.setLayoutParams(lp);
    rating.setNumStars(4);
    rating.setStepSize(1);

    //add ratingBar to linearLayout
    linearLayout.addView(rating);


    popDialog.setIcon(android.R.drawable.btn_star_big_on);
    popDialog.setTitle("Add Rating: ");

    //add linearLayout to dailog
    popDialog.setView(linearLayout);



    rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
            System.out.println("Rated val:"+v);
        }
    });



    // Button OK
    popDialog.setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    textView.setText(String.valueOf(rating.getProgress()));
                    dialog.dismiss();
                }

            })

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

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

}

关于android - 我试图在对话框中添加评级栏,但我在星数方面遇到了一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46385454/

相关文章:

javascript - 加载 jquery 对话框后更改其位置

dialog - PyQt:如何创建一个对话框作为另一个对话框的子项

c++ - 如何使用添加到 MFC Doc/Frame 项目的对话框的初始化列表

jquery - AJAX 重写后如何重新绑定(bind)对话框?

android - 是否确保在 finish() 之后调用 onDestroy()?

java - Android(Java)中另一个 jar 里的一个 jar

android - Facebook graph-api 安卓

java - 带有 OnTouch 事件的 ScrollView

android - 我的android简单计算器应用有什么问题?

jQuery 对话框第二次未打开