Android文本視圖TextView實現(xiàn)聊天室效果
本文實例為大家分享了Android文本視圖TextView實現(xiàn)聊天室的具體代碼,供大家參考,具體內(nèi)容如下
Math.random()生成隨機數(shù)的范圍是 0 到 1 之間的
日期時間格式new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); //年-月-日 時:分:秒 ; HH大寫24小時,
String類的format()方法用于創(chuàng)建格式化的字符串以及連接多個字符串對象。
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; // 聲明一個文本視圖對象
private TextView tv_control; // 聲明一個文本視圖對象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bbs);
// 從布局文件中獲取名叫tv_control的文本視圖
tv_control = findViewById(R.id.tv_control);
// 給tv_control設置點擊監(jiān)聽器
tv_control.setOnClickListener(this);
// 給tv_control設置長按監(jiān)聽器
tv_control.setOnLongClickListener(this);
// 從布局文件中獲取名叫tv_bbs的文本視圖
tv_bbs = findViewById(R.id.tv_bbs);
// 給tv_bbs設置點擊監(jiān)聽器
tv_bbs.setOnClickListener(this);
// 給tv_bbs設置長按監(jiān)聽器
tv_bbs.setOnLongClickListener(this);
// 設置tv_bbs內(nèi)部文字的對齊方式為靠左且靠下
tv_bbs.setGravity(Gravity.LEFT | Gravity.BOTTOM);
// 設置tv_bbs高度為八行文字那么高
tv_bbs.setLines(8);
// 設置tv_bbs最多顯示八行文字
tv_bbs.setMaxLines(8);
// 設置tv_bbs內(nèi)部文本的移動方式為滾動形式
tv_bbs.setMovementMethod(new ScrollingMovementMethod());
}
private String[] mChatStr = {"你吃飯了嗎?", "今天天氣真好呀。",
"我中獎啦!", "我們?nèi)タ措娪鞍?, "晚上干什么好呢?",};
@Override
public void onClick(View v) {
if (v.getId() == R.id.tv_control || v.getId() == R.id.tv_bbs) {
// 生成一個0到4之間的隨機數(shù)
int random = (int) (Math.random() * 10) % 5;
// 拼接聊天的文本內(nèi)容
String newStr = String.format("%s\n%s %s",
tv_bbs.getText().toString(), DateUtil.getNowTime(), mChatStr[random]);
// 設置文本視圖tv_bbs的文本內(nèi)容
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">
<!-- 這是普通的文本視圖 -->
<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="聊天室效果,點擊添加聊天記錄,長按刪除聊天記錄" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical">
<!-- 這是聊天室的文本視圖,scrollbars屬性設置為vertical表示在垂直方向上顯示滾動條 -->
<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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android UI之ImageView實現(xiàn)圖片旋轉(zhuǎn)和縮放
這篇文章主要介紹了Android UI之ImageView實現(xiàn)圖片旋轉(zhuǎn)和縮放的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-09-09
Android網(wǎng)絡技術(shù)HttpURLConnection詳解
這篇文章主要為大家詳細介紹了Android網(wǎng)絡技術(shù)HttpURLConnection的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Android RefreshLayout實現(xiàn)下拉刷新布局
這篇文章主要為大家詳細介紹了Android RefreshLayout實現(xiàn)下拉刷新布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Android App在線程中創(chuàng)建handler的方法講解
這篇文章主要介紹了Android App在線程中創(chuàng)建handler的方法講解,文中同時講解了handler和線程的關(guān)系以及使用Handler時一些需要注意的地方,需要的朋友可以參考下2016-03-03
android圖庫播放幻燈片時按power鍵滅屏再亮屏顯示keyguard
圖庫在播放幻燈片時,按power鍵滅屏,然后再亮屏,會發(fā)現(xiàn)幻燈片繼續(xù)在播放,沒有顯示keyguard,如何在亮屏后顯示解鎖界面,具體實現(xiàn)方法如下,感興趣的朋友可以參考下哈2013-06-06
android使用gesturedetector手勢識別示例分享
這篇文章主要介紹了android使用手勢識別的方法,介紹了單擊觸摸屏觸發(fā)的事件和雙擊事件的使用等方法,大家參考使用吧2014-01-01
Android系統(tǒng)的五種數(shù)據(jù)存儲形式實例(二)
Android系統(tǒng)有五種數(shù)據(jù)存儲形式,分別是文件存儲、SP存儲、數(shù)據(jù)庫存儲、contentprovider 內(nèi)容提供者、網(wǎng)絡存儲。本文介紹了Android系統(tǒng)的五種數(shù)據(jù)存儲形式,有興趣的可以了解一下。2016-12-12
Android使用Sensor感應器實現(xiàn)線程中刷新UI創(chuàng)建android測力計的功能
這篇文章主要介紹了Android使用Sensor感應器實現(xiàn)線程中刷新UI創(chuàng)建android測力計的功能,實例分析了Android使用Sensor感應器實現(xiàn)UI刷新及創(chuàng)建測力器的技巧,需要的朋友可以參考下2015-12-12

