From 6ebb9d50f3510d68a1cfb631766e883c29808bb0 Mon Sep 17 00:00:00 2001 From: pbmpu4aic Date: Thu, 24 Nov 2022 19:03:48 +0800 Subject: [PATCH] ADD file via upload --- CommentActivity.java | 122 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 CommentActivity.java diff --git a/CommentActivity.java b/CommentActivity.java new file mode 100644 index 0000000..36905d3 --- /dev/null +++ b/CommentActivity.java @@ -0,0 +1,122 @@ +package com.sbw.atrue.Order.Activity; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.view.View; +import android.view.inputmethod.InputMethodManager; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.ListView; +import android.widget.RelativeLayout; +import android.widget.TextView; +import android.widget.Toast; + +import com.sbw.atrue.Order.R; +import com.sbw.atrue.Order.Util.AdapterComment; +import com.sbw.atrue.Order.Entity.Comment; + +import java.util.ArrayList; +import java.util.List; + +public class CommentActivity extends Activity implements View.OnClickListener { + + private ImageView comment; + private TextView hide_down; + private EditText comment_content; + private Button comment_send; + + private LinearLayout rl_enroll; + private RelativeLayout rl_comment; + + private ListView comment_list; + private AdapterComment adapterComment; + private List data; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + initView(); + } + + private void initView() { + + // 初始化评论列表 + comment_list = (ListView) findViewById(R.id.comment_list); + // 初始化数据 + data = new ArrayList<>(); + // 初始化适配器 + adapterComment = new AdapterComment(getApplicationContext(), data); + // 为评论列表设置适配器 + comment_list.setAdapter(adapterComment); + + comment = (ImageView) findViewById(R.id.comment); + hide_down = (TextView) findViewById(R.id.hide_down); + comment_content = (EditText) findViewById(R.id.comment_content); + comment_send = (Button) findViewById(R.id.comment_send); + + rl_enroll = (LinearLayout) findViewById(R.id.rl_enroll); + rl_comment = (RelativeLayout) findViewById(R.id.rl_comment); + + setListener(); + } + + /** + * 设置监听 + */ + public void setListener(){ + comment.setOnClickListener(this); + + hide_down.setOnClickListener(this); + comment_send.setOnClickListener(this); + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.comment: + // 弹出输入法 + InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); + imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); + // 显示评论框 + rl_enroll.setVisibility(View.GONE); + rl_comment.setVisibility(View.VISIBLE); + break; + case R.id.hide_down: + // 隐藏评论框 + rl_enroll.setVisibility(View.VISIBLE); + rl_comment.setVisibility(View.GONE); + // 隐藏输入法,然后暂存当前输入框的内容,方便下次使用 + InputMethodManager im = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); + im.hideSoftInputFromWindow(comment_content.getWindowToken(), 0); + break; + case R.id.comment_send: + sendComment(); + break; + default: + break; + } + } + + /** + * 发送评论 + */ + public void sendComment(){ + if(comment_content.getText().toString().equals("")){ + Toast.makeText(getApplicationContext(), "评论不能为空!", Toast.LENGTH_SHORT).show(); + }else{ + // 生成评论数据 + Comment comment = new Comment(); + comment.setName("评论者"+(data.size()+1)+":"); + comment.setContent(comment_content.getText().toString()); + adapterComment.addComment(comment); + // 发送完,清空输入框 + comment_content.setText(""); + + Toast.makeText(getApplicationContext(), "评论成功!", Toast.LENGTH_SHORT).show(); + } + } +} \ No newline at end of file