国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

瀏覽:2日期: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大寫24小時(shí),

String類的format()方法用于創(chuàng)建格式化的字符串以及連接多個(gè)字符串對象。

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è)文本視圖對象 private TextView tv_control; // 聲明一個(gè)文本視圖對象 @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)聽器tv_control.setOnClickListener(this);// 給tv_control設(shè)置長按監(jiān)聽器tv_control.setOnLongClickListener(this);// 從布局文件中獲取名叫tv_bbs的文本視圖tv_bbs = findViewById(R.id.tv_bbs);// 給tv_bbs設(shè)置點(diǎn)擊監(jiān)聽器tv_bbs.setOnClickListener(this);// 給tv_bbs設(shè)置長按監(jiān)聽器tv_bbs.setOnLongClickListener(this);// 設(shè)置tv_bbs內(nèi)部文字的對齊方式為靠左且靠下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)擊添加聊天記錄,長按刪除聊天記錄' /> <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)聊天室效果

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

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 盐亭县| 衢州市| 西乡县| 名山县| 繁峙县| 旅游| 三河市| 平遥县| 巴彦淖尔市| 肇东市| 南昌市| 宜城市| 临汾市| 郸城县| 塔城市| 钟祥市| 疏附县| 永定县| 西华县| 名山县| 皋兰县| 北宁市| 瓦房店市| 邓州市| 怀远县| 金坛市| 昌平区| 哈巴河县| 叙永县| 友谊县| 杂多县| 沙坪坝区| 汤阴县| 彩票| 太湖县| 建德市| 古丈县| 甘洛县| 南部县| 斗六市| 太保市|