gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:34 25 4
gpt4 key购买 nike

我尝试添加有星级的评分栏,我想让价格有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com