第一步的話就是我們的布局檔案的制作:主要是我們的MainActivity檔案

布局檔案的詳細代碼:
<?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"
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: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/ic_baseline_comment_24"
android:layout_weight="1"
android:layout_gravity="center" />
<ImageView
android:id="@+id/chat"
android:layout_width="23dp"
android:layout_height="23dp"
android:src="@drawable/ic_baseline_edit_24"
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/ic_launcher_foreground"
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>
然后的話就是我們的這個啊就是item——的xml檔案:

詳細的代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<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="#000000"
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="#000000"
android:textSize="15sp" />
</LinearLayout>
然后的話就是設定我們的相關于資訊的java檔案:
`package com.example.say;
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;
}
}
`
然后的話就是我們的配接器既可以在外面單獨創建一個配接器也可以在我們的mainactivity的內部創建一個配接器:
詳細的代碼如下所示:
package com.example.say;
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 androidx.recyclerview.widget.RecyclerView;
import java.util.List;
// todo 在我們的這個位置的話就是設定我們你的配接器
public class AdapterComment extends BaseAdapter {
// 然后的話就是在我們的這個位置的話創建我們的get和我們的set方法
Context context;
List<Comment> data;
private ViewHolder holder;
// 然后的話就是設定我們的配接器
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
if(convertView == null){
holder = new ViewHolder();
// 然后的話就是將我們的xml布局檔案轉換為我們的可視化檔案
convertView = LayoutInflater.from(context).inflate(R.layout.item_comment,null);
// 這個位置的話就是找到我們的id
holder.commentName = (TextView) convertView.findViewById(R.id.comment_name);
holder.commentContent = (TextView) convertView.findViewById(R.id.comment_content);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
// 資料的適配
holder.commentContent.setText(data.get(i).getContent());
holder.commentName.setText(data.get(i).getName());
return convertView;
}
public void addComment(Comment comment){
data.add(comment);
notifyDataSetChanged();
}
//將我們要宣告的控制元件放在我們的viewholder類中,這個的話是我們的靜態類方便我們的垃圾回收
public static class ViewHolder{
TextView commentName;
TextView commentContent;
}
}
然后的話就是我們的主要的mainactivity的代碼:

主要的詳細代碼:
加粗樣式
package com.example.say;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
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 java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener {
// 現在的話就是撰寫我們的mainactivity的代碼、
private ListView commentList;
private LinearLayout rlEnroll;
private ImageView comment;
private ImageView chat;
private RelativeLayout rlComment;
private TextView hideDown;
private EditText commentContent;
private Button commentSend;
private List<Comment> data;
private AdapterComment adapterComment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 在我們的這個位置的話就是設定我們的初始化的界面的話找到我們的代碼
initView();
}
private void initView() {
// 然后的話就是設定我們的id todo 在我們的這個位置的話初始化評論串列
commentList = (ListView) findViewById(R.id.comment_list);
// todo 初始化資料
data = new ArrayList<>();
// 然后的話在我們的這個位置的話初始化我們的資料配接器
adapterComment = new AdapterComment(getApplicationContext(),data);
// 為我們的評論設定我們的配接器
commentList.setAdapter(adapterComment);
rlEnroll = (LinearLayout) findViewById(R.id.rl_enroll);
comment = (ImageView) findViewById(R.id.comment);
chat = (ImageView) findViewById(R.id.chat);
rlComment = (RelativeLayout) findViewById(R.id.rl_comment);
hideDown = (TextView) findViewById(R.id.hide_down);
commentContent = (EditText) findViewById(R.id.comment_content);
commentSend = (Button) findViewById(R.id.comment_send);
setListener();
}
// todo 在我們的這個位置的話設定我們的點擊監聽事件
public void setListener(){
comment.setOnClickListener(this);
hideDown.setOnClickListener(this);
commentSend.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
// 設定我們的輸入法
case R.id.comment:
// 然后的話在我們的這個位置彈出我們的輸入法
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
// 顯示我們的評論框
rlEnroll.setVisibility(View.GONE);
rlComment.setVisibility(View.VISIBLE);
break;
// 隱藏我們的評論框
case R.id.hide_down:
rlEnroll.setVisibility(View.VISIBLE);
rlComment.setVisibility(View.GONE);
// 隱藏我們的輸入法,然后的話暫存在我們當前的輸入框中方便下一次的使用
InputMethodManager im = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(commentContent.getWindowToken(),0);
break;
case R.id.comment_send:
// 然后在我們的這個位置的話設定一個發送評論的方法
sendComment();
break;
default:
break;
}
}
// todo 發送評論的方法
private void sendComment() {
if(commentContent.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "評論不能為空",Toast.LENGTH_LONG).show();
}else{
// 然后的話這個的話就是生成評論的數
Comment comment = new Comment();
comment.setName("評論者"+(data.size()+1)+":");
comment.setContent(commentContent.getText().toString());
// 然后的話就是適配我們的資料------todo 這里的話有部分的資料沒有撰寫
adapterComment.addComment(comment);
commentContent.setText("");
// 然后的話就是使用我們的toast的話彈出我們的成功或是失敗的評論
Toast.makeText(getApplicationContext(),"評論成功",Toast.LENGTH_LONG).show();
}
}
}
然后的話就是程式的運行效果圖:

參考來自:
參考博客:https://github.com/yeaper/android_sample/tree/master/InputDemo
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/260622.html
標籤:其他
