基于Android平台开发,在线电影购票系统(十五)影片评论实现

1. 涉及到的技术点

  1. 使用SQLite数据库存储用户评论

2. 代码实现过程

  1. 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>
  1. 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 {
                    //TODO:提交评论
                    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. 演示效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值