广告
返回顶部
首页 > 资讯 > 移动开发 >Android评论功能的实现过程
  • 414
分享到

Android评论功能的实现过程

Android 2022-06-06 07:06:46 414人浏览 泡泡鱼
摘要

目前,各种App的社区或者用户晒照片、发说说的地方,都提供了评论功能,为了更好地学习,自己把这个功能实现了一下,做了个小的Demo。 首先推荐一款实用的插件LayoutCre

目前,各种App的社区或者用户晒照片、发说说的地方,都提供了评论功能,为了更好地学习,自己把这个功能实现了一下,做了个小的Demo。

首先推荐一款实用的插件LayoutCreater,可以帮助开发者自动生成布局代码,具体用法可以去GiHub上看看:

GitHub地址:https://github.com/boredream/BorePlugin

1、新建一个Android工程,写MainActivity的布局 activity_main.xml


<RelativeLayout
 xmlns:android="Http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/grey">
 <ListView
  android:id="@+id/comment_list"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginTop="10dp"
  android:layout_marginBottom="50dp" />
 <LinearLayout
  android:id="@+id/rl_enroll"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:orientation="horizontal"
  android:layout_alignParentBottom="true"
  android:background="@color/white">
  <ImageView
   android:id="@+id/comment"
   android:layout_width="32dp"
   android:layout_height="32dp"
   android:src="@drawable/comment"
   android:layout_weight="1"
   android:layout_gravity="center" />
  <ImageView
   android:id="@+id/chat"
   android:layout_width="23Dp"
   android:layout_height="23dp"
   android:src="@drawable/chat"
   android:layout_weight="1"
   android:layout_gravity="center"/>
 </LinearLayout>
 <RelativeLayout
  android:id="@+id/rl_comment"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:background="@color/white"
  android:visibility="Gone"
  android:layout_alignParentBottom="true">
  <View
   android:layout_width="match_parent"
   android:layout_height="1dp"
   android:background="@color/grey" />
  <TextView
   android:id="@+id/hide_down"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/hide_down"
   android:textSize="13sp"
   android:textColor="@color/txtgrey"
   android:drawableBottom="@drawable/hide_dowm"
   android:layout_alignParentLeft="true"
   android:layout_centerVertical="true"
   android:layout_marginLeft="10dp"/>
  <View
   android:layout_width="1dp"
   android:layout_height="match_parent"
   android:background="@color/grey"
   android:layout_toRightOf="@id/hide_down"
   android:layout_marginLeft="10dp"/>
  <EditText
   android:id="@+id/comment_content"
   android:hint="@string/comment_content"
   android:textSize="15sp"
   android:singleLine="true"
   android:layout_width="240dp"
   android:layout_height="match_parent"
   android:background="@null"
   android:layout_toRightOf="@id/hide_down"
   android:layout_marginLeft="20dp"/>
  <Button
   android:id="@+id/comment_send"
   android:layout_width="50dp"
   android:layout_height="35dp"
   android:layout_margin="5dp"
   android:text="@string/send"
   android:textSize="13sp"
   android:textColor="@color/white"
   android:background="@color/mainColor"
   android:layout_alignParentRight="true"
   android:layout_marginRight="10dp"
   android:layout_marginLeft="15dp"/>
 </RelativeLayout>
</RelativeLayout>

2、创建评论内容实体类、 内容适配器、内容的Item布局

1)内容实体类 Comment


public class Comment {
 String name; //评论者
 String content; //评论内容
 public Comment(){
 }
 public Comment(String name, String content){
  this.name = name;
  this.content = content;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
}

2)内容适配器 AdapterComment


public class AdapterComment extends BaseAdapter {
 Context context;
 List<Comment> data;
 public AdapterComment(Context c, List<Comment> data){
  this.context = c;
  this.data = data;
 }
 @Override
 public int getCount() {
  return data.size();
 }
 @Override
 public Object getItem(int i) {
  return data.get(i);
 }
 @Override
 public long getItemId(int i) {
  return i;
 }
 @Override
 public View getView(int i, View convertView, ViewGroup viewGroup) {
  ViewHolder holder;
  // 重用convertView
  if(convertView == null){
   holder = new ViewHolder();
   convertView = LayoutInflater.from(context).inflate(R.layout.item_comment, null);
   holder.comment_name = (TextView) convertView.findViewById(R.id.comment_name);
   holder.comment_content = (TextView) convertView.findViewById(R.id.comment_content);
   convertView.setTag(holder);
  }else{
   holder = (ViewHolder) convertView.getTag();
  }
  // 适配数据
  holder.comment_name.setText(data.get(i).getName());
  holder.comment_content.setText(data.get(i).getContent());
  return convertView;
 }
 
 public void addComment(Comment comment){
  data.add(comment);
  notifyDataSetChanged();
 }
 
 public static class ViewHolder{
  TextView comment_name;
  TextView comment_content;
 }
}

3)内容的Item布局 item_comment.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <TextView
  android:id="@+id/comment_name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textColor="@color/mainColor"
  android:textSize="15sp"
  android:layout_marginLeft="15dp"
  android:layout_marginRight="3dp"/>
 <TextView
  android:id="@+id/comment_content"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textColor="@color/colorAccent"
  android:textSize="15sp" />
</LinearLayout>

3、在MainActivity选中布局,然后菜单栏点击 Code —> LayoutCreater,确定要生成的布局代码后,点击confirm完成


接下来再完善,具体的实现我已经在代码中做了注释,就不具体说了


public class MainActivity 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<Comment> 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();
  }
 }
}

注意:

因为Android 手机类型比较杂,所以有的手机中会出现底部输入框和输入法重叠,如下图,画红圈的这部分是没有的:

当出现这个问题时,可以在Manifest.xml文件中,给对应的Activity添加一条属性

android:windowsoftInputMode="stateHidden|adjustResize"

这样,输入法就可以自动调节,显示画红圈的部分,底部输入框和输入法就不会重叠了。

4、最后的效果图如下

隐藏输入框的界面

显示输入框的界面


您可能感兴趣的文章:Android实现朋友圈评论回复列表Android 仿微信朋友圈点赞和评论弹出框功能Android实现评论栏随Recyclerview滑动左右移动Android 仿今日头条评论时键盘自动弹出的效果(推荐)Android 仿抖音的评论列表的UI和效果的实现代码Android中使用PopupWindow 仿微信点赞和评论弹出Android模拟登录评论CSDN实现代码Android评论图片可移动顺序选择器(推荐)Android仿微信朋友圈点击评论自动定位到相关行功能Android如何实现社交应用中的评论与回复功能详解


--结束END--

本文标题: Android评论功能的实现过程

本文链接: https://www.lsjlt.com/news/24342.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android评论功能的实现过程
    目前,各种App的社区或者用户晒照片、发说说的地方,都提供了评论功能,为了更好地学习,自己把这个功能实现了一下,做了个小的Demo。 首先推荐一款实用的插件LayoutCre...
    99+
    2022-06-06
    Android
  • vue实现发表评论功能
    本文实例为大家分享了vue实现发表评论的具体代码,供大家参考,具体内容如下 <!DOCTYPE html> <html>     <head>  ...
    99+
    2022-11-13
  • Ajax实现评论提交功能
    本篇内容介绍了“Ajax实现评论提交功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!代码如下: docu...
    99+
    2022-10-19
  • php怎么实现评论功能
    要实现评论功能,可以使用数据库来存储评论内容,并使用PHP来处理用户的评论请求。首先,你需要创建一个数据库表来存储评论数据。可以创建...
    99+
    2023-08-11
    php
  • VUE+Java实现评论回复功能
    背景 最近需要做一个多级评论的功能,技术路线:VUE(Element)+Java(SpringBoot) 效果 后台 SQL Java Controller  @GetMapp...
    99+
    2022-11-13
  • Vue组件实现评论区功能
    本文实例为大家分享了Vue组件实现评论区的具体代码,供大家参考,具体内容如下 实现代码 <!DOCTYPE html> <html lang="en"> &...
    99+
    2022-11-13
  • 如何用vuejs实现评论功能
    这篇文章主要介绍“如何用vuejs实现评论功能”,在日常操作中,相信很多人在如何用vuejs实现评论功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何用vuejs实现评论功能”的疑惑有所帮助!接下来,请跟...
    99+
    2023-06-25
  • Vue实现简单的发表评论功能
    本文实例为大家分享了Vue实现简单的发表评论功能的具体代码,供大家参考,具体内容如下 1、这是我在学习中的实例,有些的不足的地方,还望各位大佬指点,感谢哦~ 2、发表评论的效果图 ...
    99+
    2022-11-12
  • JavaScript如何实现评论点赞功能
    这篇文章主要为大家展示了“JavaScript如何实现评论点赞功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“JavaScript如何实现评论点赞功能”这篇文...
    99+
    2022-10-19
  • vue组件实现发表评论功能
    本文实例为大家分享了vue组件实现发表评论的具体代码,供大家参考,具体内容如下 今天看了vue相关的视频,所以跟着做一个小demo把知识串联起来,内容很简单但是也算是学习道路上的一点...
    99+
    2022-11-13
  • vue怎么实现发表评论功能
    今天小编给大家分享一下vue怎么实现发表评论功能的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。具体代码如下:<!DOC...
    99+
    2023-06-30
  • Vue如何实现发表评论功能
    这篇文章主要为大家展示了“Vue如何实现发表评论功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Vue如何实现发表评论功能”这篇文章吧。具体内容如下这是我在学习中的实例,有些的不足的地方,还望...
    99+
    2023-06-25
  • Java单表实现评论回复功能
    Java单表实现评论回复功能 1.简介2.功能实现图3.数据库设计4.实体类5.实现思路6.功能实现6.1 Sql入手6.2 业务实现 7.前端实现8.最终成果 1.简介 最近在写...
    99+
    2023-08-31
    java
  • java怎么实现评论和回复功能
    这篇文章主要介绍了java怎么实现评论和回复功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇java怎么实现评论和回复功能文章都会有所收获,下面我们一起来看看吧。效果展示总共是两层回复 (回复评论、回复评论下...
    99+
    2023-07-02
  • Python Django使用forms来实现评论功能
    貌似Django从版本1.6开始就放弃了对自带的comments的使用,具体原因未查,但是现在使用Django的内部的模块也可以实现评论功能,那就是借助于forms模块,下面是我的一个小例子。 环境准备 ...
    99+
    2022-06-04
    来实现 功能 Python
  • ajax实现发表和读取评论功能
    这篇文章主要介绍“ajax实现发表和读取评论功能”,在日常操作中,相信很多人在ajax实现发表和读取评论功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”ajax实现发表和读...
    99+
    2022-10-19
  • ajax如何实现无刷新评论功能
    小编给大家分享一下ajax如何实现无刷新评论功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!这是留言板的界面,当用户点击提交留...
    99+
    2022-10-19
  • Springboot+ElementUi实现评论、回复、点赞功能
    目录1.概述2.前端代码1.html2.css3.js4.api调用后台接口3.后端代码1.数据库SQL2.实体类3.daoMapper4.daoMapper实现5.service接...
    99+
    2022-11-13
  • 怎么使用ThinkPHP来实现评论功能
    本篇内容介绍了“怎么使用ThinkPHP来实现评论功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!什么是ThinkPHP首先,让我们了解一...
    99+
    2023-07-05
  • SpringBoot整合Mybatis-plus实现多级评论功能
    目录数据库设计用户表评论表后端实现相关依赖实体类Mapper接口Service层和Controller层前端实现总结在本文中,我们将介绍如何使用SpringBoot整合Mybatis...
    99+
    2023-05-18
    SpringBoot整合Mybatis-plus SpringBoot多级评论
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作