<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@android:drawable/btn_default_small"
tools:context=".MainActivity">
<!--在我们的这个位置的话就是设置我们的就是评论功能的布局xml文件-->
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:drawable/edit_text"
android:orientation="horizontal">
<EditText
android:id="@+id/edit_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:drawable/btn_default_small" />
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="发送" />
</LinearLayout>
</RelativeLayout>
java部分的代码:
package com.example.smartcitymodel.ui.Service;
import androidx.appcompat.app.AppCompatActivity;
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.ListView;
import android.widget.Toast;
import com.example.smartcitymodel.R;
import java.util.ArrayList;
import java.util.List;
public class CommentActivity extends AppCompatActivity {
private ListView commentList;
private EditText editComment;
private Button btnSubmit;
private List<Comment> data;
private AdapterComment adapterComment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comment);
/*
* todo 在我们的这个界面的话就是我们的相关的评论界面
* */
initView();
}
private void initView() {
commentList = (ListView) findViewById(R.id.comment_list);
editComment = (EditText) findViewById(R.id.edit_comment);
btnSubmit = (Button) findViewById(R.id.btn_submit);
/*
* 在我们的这个位置的话就是设置我们的相关的方法
* */
data = new ArrayList<>();
/*
* 在我们的这个位置的话就是设置我们的相关的方法
* */
adapterComment = new AdapterComment(getApplicationContext(),data);
commentList.setAdapter(adapterComment);
/*
* 设置我们的相关的其他的方法
* */
setListener();
}
private void setListener() {
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendComment();
}
});
}
/*
*todo 点击发送我们的相关的消息
* */
private void sendComment() {
if(editComment.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "评论不能为空!", Toast.LENGTH_SHORT).show();
}else {
Comment comment = new Comment();
comment.setName("黄黄"+(data.size())+"");
comment.setContent(editComment.getText().toString());
adapterComment.addComment(comment);
// 评论发送完毕清空输入框
editComment.setText(" ");
Toast.makeText(getApplicationContext(), "评论成功!", Toast.LENGTH_SHORT).show();
}
}
}
然后的话就是设置我们的相关的comment方法:
package com.example.smartcitymodel.ui.Service;
public class Comment {
/*
* 在我们的这个位置的话就是设置我们的相关的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;
}
}
然后的话就是我们的相关的方法:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="nidemizhi"
android:id="@+id/comment_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFE500"
android:textSize="15sp"
android:layout_marginLeft="15dp"
android:layout_marginRight="3dp"/>
<TextView
android:text="tad eizhi"
android:id="@+id/comment_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00FF0A"
android:textSize="15sp" />
</LinearLayout>
然后的话还有我们的相关的代码:
package com.example.smartcitymodel.ui.Service;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.smartcitymodel.R;
import java.util.List;
public class AdapterComment extends BaseAdapter {
Context context;
List<Comment> data;
public AdapterComment(Context context, List<Comment> data) {
this.context = context;
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 view, ViewGroup viewGroup) {
ViewHolder holder;
if(view == null){
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.item_comment,null);
holder.commentName = (TextView) view.findViewById(R.id.comment_name);
holder.commentContent = (TextView) view.findViewById(R.id.comment_content);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
// 适配数据
holder.commentName.setText(data.get(i).getName());
holder.commentContent.setText(data.get(i).getContent());
// 返回view
return view;
}
/*
* 添加一条我们的数据
* */
public void addComment(Comment comment){
data.add(comment);
notifyDataSetChanged();
}
/*
* 静态的类 方便我们的gc回收
* */
public static class ViewHolder{
private TextView commentName;
private TextView commentContent;
}
}