Learning "Android Studio Development Practice" (2) - Chat Room
background
In the previous article 1 , the development of Hello World App 2 using Android Studio was realized. Here, we will continue to study the usage of Android Studio and be familiar with the use of the simple control TextView. The goal of this article is to develop a simple chat room App, to realize the function of clicking the chat room window to add a chat record, and long pressing the chat window to clear all chat records.
Writing chat room layout files
A simple chat room layout looks like this:
- Title, display a line of text in the center "chat room effect, click to add chat records, long press to delete chat records", the height is 10%, and the width is the entire screen;
- The chat record window has a height of 90% and a width of the entire screen. Click to add a random text in the chat record window and attach a time stamp. Press and hold to clear the text.
In order to realize the percentage layout, method 3 of adding a guideline (Guideline) can be used in ConstraintLayout . In this chat room, you can add a horizontal auxiliary line at the position of 0.10, so that the proportion of the title and the chat window is 10%, 90%. Then set the layout_width and layout_height of the two textviews to 0, and use the auxiliary line to limit 4 .
activity_main.xml
The full code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline_1"
app:layout_constraintGuide_percent=".10"
android:orientation="horizontal"/>
<TextView
android:id="@+id/tv_control"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="聊天室效果,点击添加聊天记录,长按删除聊天记录"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/guideline_1" />
<TextView
android:id="@+id/tv_bbs"
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="vertical"
android:textColor="#000000"
android:textSize="17sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_1"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Writing chat room code files
After the layout file activity_main.xml
is written, the code file must be written MainActivity.java
. The idea is to create two TextView
variables, onCreate()
use findViewById()
the method in the method to correspond to the two TextView
controls designed above, and then tv_bbs
bind the click and long press listeners to the control.
The complete code of MainActivity.java is as follows:
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.view.View;
import android.view.View.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.text.method.ScrollingMovementMethod;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private TextView tv_bbs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_bbs = findViewById(R.id.tv_bbs);
tv_bbs.setMovementMethod(new ScrollingMovementMethod());
OnClickListener clickT = new ClickTAction();
OnLongClickListener clickL = new ClickLAction();
tv_bbs.setOnClickListener(clickT);
tv_bbs.setOnLongClickListener(clickL);
}
private final String[] mChatStr = {
"您用餐了吗?",
"今天是个好日子。",
"我赢了!",
"我们去看电影,好吗?",
"我晚上做什么好呢?" };
private class ClickTAction implements OnClickListener {
@Override
public void onClick(View v) {
if (v.getId() == R.id.tv_bbs) {
int r = (int)(Math.random()*10) % 5;
String s = String.format("%s\n%s %s", tv_bbs.getText().toString(), getNowTime(), mChatStr[r]);
tv_bbs.setText(s);
}
}
}
private class ClickLAction implements OnLongClickListener {
@Override
public boolean onLongClick(View v) {
if (v.getId() == R.id.tv_bbs) {
tv_bbs.setText("");
}
return true;
}
}
private String getNowTime() {
SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
return s.format(new Date());
}
}
ClickTAction
and ClickLAction
are two classes that implement the interface View.OnClickListener
and respectively, and these two classes can be used as inner classes of the public class 5 .View.OnLongClickListener
MainActivity
chatStr
The array stores the chat text to be displayed, and getNowTime()
the method is a self-written function, which is used to obtain the current system time 6 .
In order to realize the display of text, the basic control TextView needs to be used. TextView is the most basic text display control 2 and is used to display text information 7 on the interface . In the layout file activity_main.xml
, you can set text
the properties of TextView to change the content of the displayed text. textColor
Attributes are used to change the color of the displayed text, and textSize
attributes are used to change the font size. scrollbars
The attribute is used to enable the text view to slide up and down, and to display the text outside the area . To add this attribute , add a sentence to the code file at the same time:
tv_bbs.setMovementMethod(new ScrollingMovementMethod());
operation result
Generate an APK file according to method 1 explored before , and then transfer it to the mobile phone to run.
Learning "Android Studio Development Practice" (1) - Hello World_Xiatangren's Blog-CSDN Blog ↩︎ ↩︎
Ouyang Shen. Android Studio Development Practice. Tsinghua University Press. 2017. ↩︎ ↩︎
Core Java Volume I - Fundamentals (10th Edition), written by Cay S. Horstmann. Translated by Zhou Lixin et al. Mechanical Industry Press. 2016.8 ↩︎
bangying4224. Android Studio development to obtain local time. CSDN blog ↩︎
Panda Brother. Android Studio Text Control TextView. CSDN Blog ↩︎