1. 涉及到的技术点
- 使用SQLite数据库存储用户评论
2. 代码实现过程
- activity_comment.xml评论布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CommentActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
app:navigationIcon="@drawable/ic_baseline_arrow_back_ios_24"
app:title="写短评"
app:titleTextColor="@color/white" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/movie_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="浏览地球"
android:textColor="#333333" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="在猫眼我追过的影片"
android:textColor="#999999"
android:textSize="10sp" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="评分"
android:textColor="#333333"
android:textSize="11sp" />
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:numStars="5"
android:stepSize="0.5" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:backgroundTint="#f5f5f5">
<EditText
android:id="@+id/et_comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="top"
android:hint="大家都再问:剧情怎么样?"
android:lineSpacingExtra="6dp"
android:padding="4dp"
android:textSize="13sp" />
</androidx.cardview.widget.CardView>
<Button
android:id="@+id/btn_comment"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_marginTop="30dp"
android:background="@drawable/login_et_bg"
android:text="评论" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
- CommentActivity.java评论页面
public class CommentActivity extends AppCompatActivity {
private TextView movie_name;
private Button btn_comment;
private EditText et_comment;
private RatingBar ratingBar;
private OrderInfo orderInfo;
private float rating = 0.0f;
;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comment);
orderInfo = (OrderInfo) getIntent().getSerializableExtra("orderInfo");
movie_name = findViewById(R.id.movie_name);
btn_comment = findViewById(R.id.btn_comment);
et_comment = findViewById(R.id.et_comment);
ratingBar = findViewById(R.id.ratingBar);
if (null != orderInfo) {
movie_name.setText(orderInfo.getMovie_name());
}
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rat, boolean b) {
rating = rat;
}
});
btn_comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String content = et_comment.getText().toString();
if (TextUtils.isEmpty(content)) {
Toast.makeText(CommentActivity.this, "评论内容不能为空", Toast.LENGTH_SHORT).show();
} else {
CommentInfo commentInfo = new CommentInfo();
commentInfo.setComment(content);
commentInfo.setRating(rating);
commentInfo.setMovie_name(orderInfo.getMovie_name());
commentInfo.setUsername(UserInfo.sUserInfo.getUsername());
CommentDbHelper.getInstance(CommentActivity.this).insertComment(commentInfo);
Toast.makeText(CommentActivity.this, "评论成功", Toast.LENGTH_SHORT).show();
finish();
}
}
});
}
}
3. 演示效果