一、文本视图TextView

聊天室跑马灯实例

1.xml

<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:gravity="center"        android:text="聊天室效果,点击添加聊天记录,长按删除聊天记录"/>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="200dp"        android:orientation="vertical">        <TextView            android:id="@+id/tv_bbs"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_marginTop="20dp"            android:scrollbars="vertical"            android:textColor="#000000"            android:textSize="17sp"/>    LinearLayout>LinearLayout>
2 activity

package com.example.kangxg.android30;import android.content.Context;import android.graphics.Color;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.format.DateUtils;import android.text.method.ScrollingMovementMethod;import android.view.Gravity;import android.view.View;import android.widget.TextView;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(8);        tv_bbs.setMaxLines(8);        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(), "time",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;    }}




更多相关文章

  1. Android聊天室(源码)
  2. 聊天室服务端
  3. Android聊天室(客户端)
  4. Android(安卓)聊天软件实现
  5. 【Android學習專題】網絡通信篇:Socket TCP(简易聊天室)
  6. 2013.09.05——— android 蓝牙聊天室之官方例子
  7. 2013.09.05——— android 蓝牙聊天室之官方例子
  8. Java基于NIO实现聊天室功能
  9. 从0实现基于Linux socket聊天室-实现聊天室的公聊、私聊功能-4

随机推荐

  1. 【iOS-cocos2d-X 游戏开发之三】Mac下配
  2. 图文来教你在eclipse下用gradle 来打包An
  3. Android(安卓)API 实验记录(一)
  4. ImageSwitcher的应用
  5. 谷歌I/O 2011开发者大会现场报道
  6. 「Android集成微信支付」面试题回答思路
  7. android穿越之旅--如何弹出一个非比寻常
  8. Android中BroadcastReceiver的运用
  9. android 入门不顺啊,解压android_gingerbr
  10. Android(安卓)Binder IPC分析