Android 开发笔记___textview_聊天室效果

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <TextView
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:id="@+id/tv_control"
10         android:layout_marginTop="20dp"
11         android:gravity="center"
12         android:text="聊天室效果,点击添加聊天记录,长按删除聊天记录"
13         />
14 
15     <LinearLayout
16         android:orientation="vertical"
17         android:layout_width="match_parent"
18         android:layout_height="200dp">
19 
20         <TextView
21             android:layout_width="match_parent"
22             android:layout_height="match_parent"
23             android:id="@+id/tv_bbs"
24             android:layout_marginTop="20dp"
25             android:scrollbars="vertical"
26             android:textColor="@color/text_color"
27             android:textSize="17sp"/>
28 
29     </LinearLayout>
30 
31 </LinearLayout>

java

 1 package com.example.alimjan.hello_world;
 2 
 3 import android.app.Activity;
 4 import android.content.Context;
 5 import android.content.DialogInterface;
 6 import android.content.Intent;
 7 import android.os.Bundle;
 8 import android.support.annotation.Nullable;
 9 import android.support.v7.app.AppCompatActivity;
10 import android.text.format.DateUtils;
11 import android.text.method.ScrollingMovementMethod;
12 import android.view.Gravity;
13 import android.view.View;
14 import android.widget.TextView;
15 
16 /**
17  * Created by alimjan on 6/30/2017.
18  */
19 
20 public class class__2_3_1_2 extends AppCompatActivity implements DialogInterface.OnClickListener,View.OnLongClickListener, View.OnClickListener {
21 
22     private TextView tv_bbs,tv_control;
23 
24     @Override
25     protected void onCreate(@Nullable Bundle savedInstanceState) {
26         super.onCreate(savedInstanceState);
27         setContentView(R.layout.code_2_3_1_2);
28 
29         tv_control = (TextView) findViewById(R.id.tv_control);
30         tv_control.setOnClickListener(this);
31         tv_control.setOnLongClickListener(this);
32         tv_bbs = (TextView) findViewById(R.id.tv_bbs);
33         tv_bbs.setOnClickListener(this);
34         tv_bbs.setOnLongClickListener(this);
35         tv_bbs.setGravity(Gravity.LEFT|Gravity.BOTTOM);
36         tv_bbs.setLines(8);
37         tv_bbs.setMaxLines(8);
38         tv_bbs.setMovementMethod(new ScrollingMovementMethod());
39     }
40 
41     private String[] mChatStr={"你吃饭了吗honey?","今天天气超级热!你们那儿呢?","我中奖啦!","走不走看电影~","你在干什么?"};
42 
43     public static void startHome(Context mContext) {
44         Intent intent = new Intent(mContext, class__2_3_1_2.class);
45         mContext.startActivity(intent);
46     }
47 
48     @Override
49     public void onClick(DialogInterface dialog, int which) {
50 
51     }
52 
53     @Override
54     public boolean onLongClick(View v) {
55         if(v.getId() == R.id.tv_control || v.getId() == R.id.tv_bbs){
56             tv_bbs.setText("");
57         }
58         return true;
59     }
60 
61     @Override
62     public void onClick(View v) {
63         if(v.getId() == R.id.tv_control || v.getId() == R.id.tv_bbs){
64             int random = (int) ((Math.random()*10)%5);
65             String newstr = String.format("%s
%s%s",tv_bbs.getText().toString(), DateUtil.getCurDateStr(),mChatStr[random]);
66             tv_bbs.setText(newstr);
67         }
68     }
69 }
原文地址:https://www.cnblogs.com/alimjan/p/7101729.html