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

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

