【发布时间】:2020-07-28 10:51:11
【问题描述】:
我是一名初级程序员,我正在开发一个显示一些项目的应用程序。用户可以点击任何项目,阅读它的描述,也可以对它进行评分和评论。我创建了一个评分栏和一个 cmets 部分(以及一个显示平均评分的文本视图)。
我希望在每个用户按下按钮 mSendFeedback 时保存他们的评分和评论。我怎样才能做到这一点?
项目页面的Java文件
public class ItemDemo extends AppCompatActivity {
RatingBar mRatingBar;
TextView mRatingScale;
TextView averageRating;
EditText mFeedback;
Button mSendFeedback;
int i=0;
float total = 0;
float average;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_demo);
mRatingBar = (RatingBar) findViewById(R.id.ratingBar);
mRatingScale = (TextView) findViewById(R.id.tvRatingScale);
mFeedback = (EditText) findViewById(R.id.etFeedback);
mSendFeedback = (Button) findViewById(R.id.btnSubmit);
mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
mRatingScale.setText(String.valueOf(v));
switch ((int) ratingBar.getRating()) {
case 1:
mRatingScale.setText("Bad");
break;
case 2:
mRatingScale.setText("Okay");
break;
case 3:
mRatingScale.setText("Not bad");
break;
case 4:
mRatingScale.setText("Good enough");
break;
case 5:
mRatingScale.setText("Perfect");
break;
default:
mRatingScale.setText("");
}
}
});
mSendFeedback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mRatingBar.getRating() == 0.0) {
Toast.makeText(EventDemo.this, "Please enter a rating", Toast.LENGTH_LONG).show();
} else {
mFeedback.setText("");
mRatingBar.setRating(0);
i=i+1;
total += mRatingBar.getRating();
average = total /i ;
Toast.makeText(EventDemo.this, "Thanks for the rating", Toast.LENGTH_SHORT).show();
}
}
});
TextView textView = (TextView) findViewById(R.id.averageRating);
textView.setText("average rating: " +average+"/5 stars");
}
}
项目页面的 XML 文件
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="177dp"
app:srcCompat="@drawable/item1" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView4"
android:layout_marginTop="19dp"
android:gravity="center|center_horizontal"
android:text="Item 1"
android:textAllCaps="false"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textColor="@color/design_default_color_primary_dark"
android:textColorHighlight="@color/colorPrimaryDark"
android:textColorLink="@color/design_default_color_primary"
android:textStyle="bold"
tools:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView11"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="This is the description of item 1"
android:textColor="@color/colorAccent"
android:textStyle="italic"
tools:layout_centerHorizontal="true" />
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView12"
android:layout_gravity="center"
android:numStars="5"
android:stepSize="1" />
<TextView
android:id="@+id/tvRatingScale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ratingBar"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/averageRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvRatingScale"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textStyle="bold" />
<EditText
android:id="@+id/etFeedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/averageRating"
android:gravity="top"
android:hint="add a comment"
android:inputType="textMultiLine"
android:lines="5" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#e57373"
android:text="Submit Feedback"
android:textColor="@android:color/white" />
【问题讨论】:
标签: java android button save comments