成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android文本視圖TextView實(shí)現(xiàn)聊天室效果

瀏覽:5日期:2022-09-18 15:15:33

本文實(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 implementsView.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('%sn%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'> <!-- 這是普通的文本視圖 --> <TextViewandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginTop='20dp'android:gravity='center'android:text='聊天室效果,點(diǎn)擊添加聊天記錄,長(zhǎng)按刪除聊天記錄' /> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='200dp'android:orientation='vertical'> <!-- 這是聊天室的文本視圖,scrollbars屬性設(shè)置為vertical表示在垂直方向上顯示滾動(dòng)條 --><TextView android: 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

Android文本視圖TextView實(shí)現(xiàn)聊天室效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章: