Android 系列 6.23让他们看打星标:使用RatingBar

来源:互联网 发布:苹果cms制作解析接口 编辑:程序博客网 时间:2024/06/14 08:37
6.23让他们看打星标:使用RatingBar
问题
您希望用户从组中的多个相同的GUI元素中进行选择,以指示诸如“评级”或“评估”的值。

使用RatingBar小部件;它可以指定要显示的星数和默认评级,当用户更改该值时通知您,并让您检索评分。
讨论
RatingBar提供了新近熟悉的“评级”用户界面体验,其中用户被要求使用星级分类对评分或评分(RatingBar不显示要评分的内容;这取决于您的应用程序的其余部分)。 RatingBar是ProgressBar的子类,扩展为在栏中显示整个数量的图标(“星星”)。其主要属性有:
numStars
要显示的星星数(int)
rating
用户选择的评分(float,因为stepSize)
stepSize
选择的增量(float,common值为1.0和0.5,取决于您希望评级的细致程度)
isIndicator
布尔值,设置为true以使其为只读
这些属性通常在XML中设置:
<RatingBarandroid:id="@+id/serviceBar"android:gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:numStars="5"android:rating="3"android:stepSize="1.0"android:isIndicator='false'/>
RatingBar在内部维护其评级值。您可以通过两种方式了解用户对项目的评分:
•调用getRating()方法。
•提供OnRatingBarChangeListener类型的更改通知侦听器。
OnRatingBarChangeListener有一个方法onRatingChanged,使用三个参数调用:
评分
事件源,对特定RatingBar的引用
float fRating
设置的评分
boolean fromUser
如果由用户设置,则为true,如果以编程方式设置,则为false
例6-28模拟客户调查;它创建两个RatingBar,一个用于评价服务,另一个用于评价价格(除了android:id之外,两者的XML是相同的)。在主程序中,创建一个OnRatingBarChangeListener,以显示给定评级的触摸反馈(评级转换为int,switch语句用于生成Toast的消息)。

实例6-28。 RatingBar演示应用程序

public class MainActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);OnRatingBarChangeListener barChangeListener = new OnRatingBarChangeListener() {@Overridepublic void onRatingChanged(RatingBar rBar, float fRating, boolean fromUser) {int rating = (int) fRating;String message = null;switch(rating) {case 1: message = "Sorry you're really upset with us"; break;case 2: message = "Sorry you're not happy"; break;case 3: message = "Good enough is not good enough"; break;case 4: message = "Thanks, we're glad you liked it."; break;case 5: message = "Awesome - thanks!"; break;}Toast.makeText(Main.this,message,Toast.LENGTH_LONG).show();}}; final RatingBar sBar = (RatingBar) findViewById(R.id.serviceBar);sBar.setOnRatingBarChangeListener(barChangeListener);final RatingBar pBar = (RatingBar) findViewById(R.id.priceBar);pBar.setOnRatingBarChangeListener(barChangeListener);Button doneButton = (Button) findViewById(R.id.doneButton);doneButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {String message = String.format("Final Answer: Price %.0f/%d, Service %.0f/%d%nThank you!",sBar.getRating(), sBar.getNumStars(),pBar.getRating(), pBar.getNumStars());// Thank the userToast.makeText(Main.this,message,Toast.LENGTH_LONG).show();// And upload the numbers to a database, hopefully...// That's all for this Activity, hence this App.finish();}});}}
有多个RatingBar,所以我们不保存在监听器的值,因为不完整的调查没有用; 在完成按钮动作侦听器中,我们获取这两个值并显示它们,这将是保存它们的地方。 您的里程可能不同:将它们保存在OnRatingBarChangeListener中可能更有意义。
如果你不习惯printf类似的格式化,String.format调用使用%.0f格式化float作为一个int,而不是投射它(因为我们必须做很好的格式化)。 理想情况下,格式消息应该来自XML字符串,但这只是一个演示程序。
主界面如图6-16所示。


图6-16。 显示反馈评级
当用户单击完成按钮时,她将在桌面窗口上看到“告别”消息(见图6-17)。


图6-17。 完成评级/调查
当希望显示来自社区的当前“平均”或类似度量评级并允许用户输入她自己的评级时,通常以只读的方式显示当前评分,并创建弹出对话框以输入 用户的特定评级。 这在Android模式网站上描述。

0 0