Android 简单视图

一、文本视图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;
    }

}


 
 

你可能感兴趣的:(Android)