标签:文字 draw date() 14. 长按删除 dateutil extend 电影 %s
窗口的高度是固定的,新的文字消息总是加入窗口末尾,同时窗口内部的文本整体向上滚动,窗口的大小、位置固定不变
<?xml version="1.0" encoding="utf-8"?> <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:layout_marginBottom="20dp" android:gravity="center" android:text="聊天室效果,点击添加聊天记录,长按删除聊天记录" /> <LinearLayout android:layout_width="match_parent" android:layout_height="200dp" android:orientation="vertical" android:background="@drawable/shape"> <TextView android:id="@+id/tv_bbs" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:scrollbars="vertical" android:textColor="#000000" android:textSize="17sp" /> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#FFFFFF" /> <stroke android:width="1dp" android:color="#000000" /> <padding android:bottom="0dp" android:left="10dp" android:right="10dp" android:top="0dp" /> </shape>
package com.example.chatroom; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.method.ScrollingMovementMethod; import android.view.Gravity; import android.view.View; import android.widget.TextView; import com.example.chatroom.util.DateUtil; public class MainActivity 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_main); tv_control = findViewById(R.id.tv_control); tv_control.setOnClickListener(this); tv_control.setOnLongClickListener(this); tv_bbs = findViewById(R.id.tv_bbs); tv_bbs.setOnClickListener(this); tv_bbs.setOnLongClickListener(this); tv_bbs.setGravity(Gravity.LEFT|Gravity.BOTTOM); tv_bbs.setLines(9); tv_bbs.setMaxLines(9); tv_bbs.setMovementMethod(new ScrollingMovementMethod()); } private String[] mChatStr = { "你吃饭了吗?", "今天天气真好呀。", "我中奖啦!", "我们去看电影吧", "晚上干什么好呢?", }; @Override public void onClick(View v) { if (v.getId() == R.id.tv_control || v.getId() == R.id.tv_bbs) { int random = (int)(Math.random()*10) % 5; String newStr = String.format("%s\n%s %s", tv_bbs.getText().toString(), DateUtil.getNowTime(), mChatStr[random]); 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; } }
package com.example.chatroom.util; import android.annotation.SuppressLint; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by ouyangshen on 2016/9/14. */ public class DateUtil { @SuppressLint("SimpleDateFormat") public static String getNowDateTime() { SimpleDateFormat s_format = new SimpleDateFormat("yyyyMMddhhmmss"); return s_format.format(new Date()); } @SuppressLint("SimpleDateFormat") public static String getNowTime() { SimpleDateFormat s_format = new SimpleDateFormat("HH:mm:ss"); return s_format.format(new Date()); } }
标签:文字 draw date() 14. 长按删除 dateutil extend 电影 %s
原文地址:https://www.cnblogs.com/soldierback/p/10810113.html