<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_control"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="聊天室效果,点击添加聊天记录,长按删除聊天记录"
/> <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv_bbs"
android:layout_marginTop="20dp"
android:scrollbars="vertical"
android:textColor="@color/text_color"
android:textSize="17sp"/> </LinearLayout> </LinearLayout>
java
package com.example.alimjan.hello_world; import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView; /**
* Created by alimjan on 6/30/2017.
*/ public class class__2_3_1_2 extends AppCompatActivity implements DialogInterface.OnClickListener,View.OnLongClickListener, View.OnClickListener { private TextView tv_bbs,tv_control; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_2_3_1_2); tv_control = (TextView) findViewById(R.id.tv_control);
tv_control.setOnClickListener(this);
tv_control.setOnLongClickListener(this);
tv_bbs = (TextView) findViewById(R.id.tv_bbs);
tv_bbs.setOnClickListener(this);
tv_bbs.setOnLongClickListener(this);
tv_bbs.setGravity(Gravity.LEFT|Gravity.BOTTOM);
tv_bbs.setLines(8);
tv_bbs.setMaxLines(8);
tv_bbs.setMovementMethod(new ScrollingMovementMethod());
} private String[] mChatStr={"你吃饭了吗honey?","今天天气超级热!你们那儿呢?","我中奖啦!","走不走看电影~","你在干什么?"}; public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class__2_3_1_2.class);
mContext.startActivity(intent);
} @Override
public void onClick(DialogInterface dialog, int which) { } @Override
public boolean onLongClick(View v) {
if(v.getId() == R.id.tv_control || v.getId() == R.id.tv_bbs){
tv_bbs.setText("");
}
return true;
} @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.getCurDateStr(),mChatStr[random]);
tv_bbs.setText(newstr);
}
}
}