Android不点赞/踩不让提交评论

当我接到这个需求的时候就觉得坑人,为啥呢?先听听需求方怎么说,需求方说的是网上有很多人点的是赞,但是评论的内容的负面的,因此需要审核后才能公布。我又为什么觉得坑人呢?这样做会导致评论失去客观性,商家想让好评出现就让好评出现,想让差评消失就不然差评显示。

但是作为一个程序猿,上级交代的命令敢不服从吗?于是这个页面是这么写的,先看布局文件:

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

    <include layout="@layout/a_title_bar"/>

    <include
        layout="@layout/layout_loading"
        android:visibility="gone"/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="1dp"
            android:background="#AAA"/>



      <LinearLayout
          android:layout_marginTop="12dp"
          android:orientation="vertical"
          android:background="#FFFFFF"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
              <TextView
                  android:paddingLeft="10dp"
                  android:layout_width="wrap_content"
                  android:textColor="#000"
                  android:text="(评论 "
                  android:textSize="18dp"
                  android:layout_height="wrap_content" />
              <ImageView
                  android:id="@+id/type_detail_im_dianzan"
                  android:background="@mipmap/zan"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text=" 赞     "/>

              <ImageView
                  android:id="@+id/type_detail_im_cai"
                  android:background="@mipmap/cai"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />

              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text=" 踩"/>

              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="#000"
                  android:textSize="18dp"
                  android:text=")"/>

          </LinearLayout>
          <TextView
              android:layout_marginTop="12dp"
              android:layout_width="match_parent"
              android:layout_height="1dp"
              android:background="#80AAAAAA"/>

          <EditText
              android:layout_marginTop="8dp"
              android:id="@+id/comment_say"
              android:gravity="top|left"
              android:hint="@string/edit_information"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textSize="16dp"
              android:background="@drawable/bg_white_roundcorner"
              android:inputType="textMultiLine"
              android:minLines="6" />

          <TextView
              android:id="@+id/num_limit"
              android:paddingRight="8dp"
              android:textSize="16dp"
              android:gravity="right"
              android:text="(0/500)"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
      </LinearLayout>

        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFFFF"
            android:paddingTop="80dp"
            android:orientation="vertical">
            <Button

                android:id="@+id/comment_commit"
                android:layout_width="wrap_content"
                android:layout_height="45dp"
                android:layout_gravity="center"
                android:layout_marginLeft="40dp"
                android:layout_marginRight="40dp"
                android:text="提交"
                android:background="@mipmap/button_bg"
                android:textColor="@color/white"
                android:textSize="20dp"/>
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

当然,提交按钮和赞/踩的图标还是得自己做,接下来看代码:

/**
 * Created by Luyifei on 2016-9-5.
 */
public class CommentActivity extends BaseActivity implements View.OnClickListener {
    private int limit = 500;//字数限制500
    private int num  = 0;//当前字数
    private TextView num_limit;
    private Button comment_commit;
    private EditText comment_say;
    private ImageView type_detail_im_dianzan,type_detail_im_diancai;
    private String comment;
    private int yd=0;//0表示没点过赞和踩,1表示点过赞和踩
    int flag ;//flag = 1是赞,= 2是踩

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(this).inflate(R.layout.activity_comment,null);
        setContentView(view);
        updateSubTitleBar("评论", -1, null);
        comment_commit = (Button) findViewById(R.id.comment_commit);//提交
        comment_commit.setOnClickListener(this);
        type_detail_im_dianzan = (ImageView) findViewById(R.id.type_detail_im_dianzan);
        type_detail_im_dianzan.setOnClickListener(this);
        type_detail_im_diancai = (ImageView) findViewById(R.id.type_detail_im_cai);
        type_detail_im_diancai.setOnClickListener(this);
        comment_say = (EditText) findViewById(R.id.comment_say);
        num_limit = (TextView) findViewById(R.id.num_limit);
        comment_say.setFilters(new InputFilter[]{new InputFilter.LengthFilter(500)});
        comment_say.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //s:变化前的所有字符; start:字符开始的位置; count:变化前的总字节数;after:变化后的字节数
                num = s.length();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //S:变化后的所有字符;start:字符起始的位置;before: 变化之前的总字节数;count:变化后的字节数
                num = s.length();

                num_limit.setText("("+num+"/"+limit+")");
            }

            @Override
            public void afterTextChanged(Editable s) {
                //s:变化后的所有字符
                if (num > 480){
                    num_limit.setTextColor(Color.RED);
                }else if (num <= 480){
                    num_limit.setTextColor(Color.BLACK);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.comment_commit:
                if (flag == 0){
                    ToastUtil.showShortToast("您没点赞或踩,不能提交评论");
                }else {
		//在此处做你想做的事情
                    finish();
                }

                break;
            case R.id.type_detail_im_dianzan:
                if (flag == 2){//已经点了踩
                    ToastUtil.showShortToast("您已点过踩,不能再赞了~");
                }else {
                    if (yd == 1){
                        type_detail_im_dianzan.setImageResource(R.mipmap.zan);
                        yd = 2;
                        flag = 0;
                    }else {
                        type_detail_im_dianzan.setImageResource(R.drawable.zan_press);
                        yd = 1;
                        flag = 1;
                    }
                }
                break;
            case R.id.type_detail_im_cai:
                if (flag == 1){//已经点了赞
                    ToastUtil.showShortToast("您已点过赞,不能再踩了~");
                }else {
                    if (yd == 1){
                        type_detail_im_diancai.setImageResource(R.mipmap.cai);
                        yd = 2;
                        flag = 0;
                    }else {
                        type_detail_im_diancai.setImageResource(R.drawable.cai_press);
                        yd = 1;
                        flag = 2;
                    }
                }
                break;
        }
    }
}
这样做的效果是你点了赞可以取消,点了踩也可以取消,但是不点赞不点踩不让提交评论。

 
 

 
 
我是一名刚入门的安卓小菜鸟,能力有限,如果对你有帮助请点赞,如果有错误的地方还请指正。
我会把我工作中遇到的问题写下来供像我这样的菜鸟参考,一起努力提升自己。谢谢观看!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值