Android text view textview to achieve chat room effect

Time:2021-7-23

This example shares the specific code of Android text view textview to realize chat room for your reference. The specific contents are as follows

Math. Random() generates random numbers in the range of 0 to 1

Date time format NEW   SimpleDateFormat(“dd-MM-yyyy HH:mm:ss”); // Year month day hour: minute: Second  ; HH in words 24 hours,

The format () method of the string class is used to create a formatted string and connect multiple string objects.

MainActivity

package com.example.junior;
 
import android.os.Bundle;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
 
import com.example.junior.util.DateUtil;
 
public class BbsActivity extends AppCompatActivity implements
        View.OnClickListener, View.OnLongClickListener {
    private TextView tv_ bbs; //  Declare a text view object
    private TextView tv_ control; //  Declare a text view object
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bbs);
        //Get TV from the layout file_ Text view of control
        tv_control = findViewById(R.id.tv_control);
        //For TV_ Control set click listener
        tv_control.setOnClickListener(this);
        //For TV_ Control set long press monitor
        tv_control.setOnLongClickListener(this);
        //Get TV from the layout file_ Text view of BBS
        tv_bbs = findViewById(R.id.tv_bbs);
        //For TV_ BBS set click listener
        tv_bbs.setOnClickListener(this);
        //For TV_ BBS set long press monitor
        tv_bbs.setOnLongClickListener(this);
        //Set up TV_ BBS internal text alignment is left and bottom
        tv_bbs.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        //Set up TV_ The height of BBS is as high as eight lines of text
        tv_bbs.setLines(8);
        //Set up TV_ BBS displays up to eight lines of text
        tv_bbs.setMaxLines(8);
        //Set up TV_ The moving mode of BBS internal text is scrolling
        tv_bbs.setMovementMethod(new ScrollingMovementMethod());
    }
 
    Private string [] mchatstr = {"have you eaten yet?"“ It's a nice day today. ",
            "I won the lottery!"“ Let's go to the cinema;
 
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_control || v.getId() == R.id.tv_bbs) {
            //Generate a random number between 0 and 4
            int random = (int) (Math.random() * 10) % 5;
            //Splicing chat text content
            String newStr = String.format("%s\n%s %s",
                    tv_bbs.getText().toString(), DateUtil.getNowTime(), mChatStr[random]);
            //Set text view TV_ Text content of BBS
            tv_bbs.setText(newStr);
        }
    }
 
    @Override
    public boolean onLongClick(View v) {
        if (v.getId() == R.id.tv_control || v.getId() == R.id.tv_bbs) {
            tv_bbs.setText("");
        }
        return true;
    }
}

layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <!--  This is a normal text view -- >
    <TextView
        android:id="@+id/tv_control"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        Android: text = "chat room effect, click Add chat record, long press to delete chat record" / >
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical">
 
        <!--  This is the text view of the chat room. If the scrollbars property is set to vertical, the scroll bar will be displayed in the vertical direction
        <TextView
            android:id="@+id/tv_bbs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="20dp"
            android:gravity="left|bottom"
            android:lines="8"
            android:maxLines="8"
            android:scrollbars="vertical"
            android:textColor="#000000"
            android:textSize="17sp" />
    </LinearLayout>
</LinearLayout>

DataUtil


package com.example.junior.util;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class DateUtil {
    public static String getNowDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }
 
    public static String getNowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(new Date());
    }
 
}

result

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.