gpt4 book ai didi

android - 评级栏不工作

转载 作者:太空狗 更新时间:2023-10-29 14:13:40 25 4
gpt4 key购买 nike

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

我试过几个教程,但还是没用。

这是我的代码:

布局代码:

<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" />

Activity :

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();

}
}

我也试过:

Activity :

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);
}

还有:

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 上放置了一个断点,当我触摸星星时没有进入该方法,也没有显示 Toast。

谢谢大家

最佳答案

试试这个:

ma​​in.xml

<?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

 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/

希望这对您有所帮助。

关于android - 评级栏不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24585174/

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