首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RatingBar不工作

RatingBar不工作
EN

Stack Overflow用户
提问于 2014-07-05 17:53:09
回答 2查看 7.3K关注 0票数 3

我在我的应用程序中有一个简单的评分栏,但当我触摸星标什么都不做时,它应该很容易,但它不起作用……

我已经尝试了几个教程,但也不起作用。

下面是我的代码:

布局代码:

代码语言:javascript
复制
<RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativeLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" 
        android:isIndicator="false"
        android:stepSize="1.0" 
        android:rating="2.0"
        android:numStars="5" 
        android:paddingBottom="5dp" />

活动:

代码语言:javascript
复制
public final class ViewActivity extends Activity implements OnRatingBarChangeListener {

RatingBar ratingbar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
    ratingbar = (RatingBar) findViewById(R.id.ratingBar1);
    ratingbar.setOnRatingBarChangeListener(this);
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
        boolean fromUser) {
    Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show();

}
}

我还试着这样做:

活动:

代码语言:javascript
复制
public final class ViewActivity extends Activity implements OnRatingBarChangeListener {

RatingBar ratingbar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
    ratingbar = (RatingBar) findViewById(R.id.ratingBar1);
OnRatingBarChangeListener rating = new OnRatingBarChangeListener() {    

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser) {
            Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show();
        }
    };
    final RatingBar sBar = (RatingBar) findViewById(R.id.ratingBar1);
    sBar.setOnRatingBarChangeListener(rating);
}

还有:

代码语言:javascript
复制
public final class ViewActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
RatingBar rating = (RatingBar)findViewById(R.id.ratingBar1);// create RatingBar object
    rating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser) {
            Toast.makeText(getApplicationContext(),Float.toString(rating),Toast.LENGTH_LONG).show();
        }
    });

我使用了调试器,并为Toast设置了一个断点,当我触摸到stars时,它不会进入方法,也不会显示Toast。

谢谢你们所有人。

EN

回答 2

Stack Overflow用户

发布于 2014-07-05 18:12:43

试试这个:

main.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#C2C2A3">

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_marginTop="50dp"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:text="OK" />

MainActivity.java

代码语言:javascript
复制
 public class MainActivity extends Activity
 {
    RatingBar ratingBar;
    Button btn;
 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   ratingBar=(RatingBar)findViewById(R.id.ratingBar1);
   btn=(Button)findViewById(R.id.button1);

   // Set ChangeListener to Rating Bar
    ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar, float rating,
         boolean fromUser) {

         Toast.makeText(getApplicationContext(),"Your Selected Ratings  : " + String.valueOf(rating),Toast.LENGTH_LONG).show();

        }
       });

    btn.setOnClickListener(new View.OnClickListener() {


 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  float rating=ratingBar.getRating(); 
  Toast.makeText(getApplicationContext(),"Your Selected Ratings  : " + String.valueOf(rating),Toast.LENGTH_LONG).show();
 }
});

 }  
 }

此外,请查看以下链接:

  1. http://www.javatpoint.com/android-rating-bar-example
  2. http://webtutsdepot.com/2011/08/20/android-sdk-tutorial-rating-bar-example/

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2018-11-23 03:54:58

属性isIndicator必须为false。

您可以通过编程使用setter来完成此操作:

代码语言:javascript
复制
    RatingBar ratingBar = findViewById(R.id.YOURID);
    ratingBar.setIsIndicator(false);

或在xml属性中:

代码语言:javascript
复制
    android:isIndicator="false"

确保您获得的是正确的RatingBar。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24585174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档