自定义view

目录

 

 

//手势拖动

//换背景色和随机数

//扇形(转盘)

//按钮图片切换

//转盘和文字

//流式布局


 

//手势拖动

package com.bawei.myapplication.gesture;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * 这里是手势拖动
 * @author hasee
 */
public class CustomGestureView extends View{
    Paint mPaint;
    float mTouchX = 300, mTouchY = 300, mCircleRadius = 50;

    public CustomGestureView(Context context) {
        super(context);
        init();
    }

    public CustomGestureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(10);
        mPaint.setStyle(Paint.Style.FILL);

        //监听touch事件
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //获取点击位置
                mTouchX = event.getX();
                mTouchY = event.getY();

                //刷新view
                invalidate();

                return true;
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画圆
        //第一个参数,圆心x轴位置
        //第二个参数,圆心y轴位置
        //第三个参数,圆形半径
        //第四个参数,画笔
        canvas.drawCircle(mTouchX, mTouchY, mCircleRadius, mPaint);
    }
}

//换背景色和随机数

package com.bawei.myapplication.random;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import java.util.Random;

/**
 * @author hasee
 */
@SuppressLint("AppCompatCustomView")
public class CustomRandomTextView extends TextView {
    public CustomRandomTextView(Context context) {
        super(context);
        init();
    }

    public CustomRandomTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        changeText();
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                changeText();
            }
        });
    }

    int i = 0;

    private void changeText(){
        setText(String.valueOf(getRandom()));
        if(i % 2 == 0){
            setBackgroundColor(Color.BLACK);
        }else{
            setBackgroundColor(Color.RED);
        }

        i++;
    }

    private int getRandom(){
        //获取随机数Math.random(),取值范围[0, 1);
        //所有*9000后取值范围 [0, 9000)
        //最后加上1000,范围[1000, 10000)
       return (int)(Math.random() * 9000 + 1000);
    }
}
/*
*int flag = new Random().nextInt(9999);
        if (flag < 1000) {
            flag += 1000;
        }
*/

//扇形(转盘)

package com.bawei.myapplication.sector_graph;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.bawei.myapplication.R;

/**
 * 画饼图的view
 * @author hasee
 */
public class CustomSectorGraphView extends View{
    /**
     * 记录传入的角度
     */
    float[] mAngles;
    /**
     * 位置控制
     */
    RectF mRectF;

    /**
     * 画笔
     */
    Paint mPaint;

    /**
     * 起始角度
     */
    float mStartAngle = 0;

    Context mContext;

    int mWidth, mHeight;

    public CustomSectorGraphView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public CustomSectorGraphView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }

    /**
     * 设置每个扇形角度
     * @param angles 角度数组
     */
    public void setData(float[] angles){
        mAngles = angles;
    }

    /**
     * 初始化
     */
    private void init() {
        //初始化画笔
        mPaint = new Paint();
        //设置画笔颜色为蓝色
        mPaint.setColor(Color.BLUE);
        //设置宽度
        mPaint.setStrokeWidth(10);
        //设置填充模式
        mPaint.setStyle(Paint.Style.FILL);


        mWidth = mContext.getResources().getDimensionPixelOffset(R.dimen.circle_width);
        mHeight = mContext.getResources().getDimensionPixelOffset(R.dimen.circle_height);

        //设置上下左右位置
        mRectF = new RectF();
        mRectF.top = 0;
        mRectF.bottom = mRectF.top + mHeight;
        mRectF.right = mRectF.left + mWidth;
        mRectF.left = 0;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for(int i = 0; i  <  mAngles.length; i++){
            if(i % 2 == 0 ){
                //角标为双数时,设置画笔颜色为蓝色
                mPaint.setColor(Color.BLUE);
            }else{
                //角标为单数时,设置画笔颜色为绿色
                mPaint.setColor(Color.GREEN);
            }

            //画扇形
            //第一个参数,位置信息
            //第二个参数,起始角度
            //第三个参数,从起始角度开始,顺时针旋转角度
            //第四个参数,是否延申到圆心
            //第五个参数,画笔
            canvas.drawArc(mRectF, mStartAngle, mAngles[i], true, mPaint);

            //下一个扇形的起始角度 = 本次扇形起始角度 + 本次扇形旋转角度
            mStartAngle += mAngles[i];
        }
    }
}

//按钮图片切换

package com.bawei.myapplication.switch_button;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bawei.myapplication.R;

/**
 * @author hasee
 */
public class CustomSwitchButtonViewGroup extends RelativeLayout{
    Context mContext;
    boolean isChecked = false;

    public CustomSwitchButtonViewGroup(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public CustomSwitchButtonViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        final ImageView imageView = new ImageView(mContext);
        imageView.setBackgroundResource(R.drawable.off);

        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isChecked){
                    imageView.setBackgroundResource(R.drawable.off);
                }else{
                    imageView.setBackgroundResource(R.drawable.on);
                }
                isChecked = !isChecked;
            }
        });

        addView(imageView);
    }
}

//转盘和文字

package com.bawei.myapplication.turntable;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.bawei.myapplication.R;

/**
 * 转盘中间开始按钮和指针
 * @author hasee
 */
public class CustomTurntableInsideView extends View {
    /**
     * 画笔
     */
    Paint mPaint;
    RectF mRectF;
    String mStr;

    public CustomTurntableInsideView(Context context) {
        super(context);
        init();
    }

    public CustomTurntableInsideView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        //自定义属性,如何添加自定义属性如下(考点)
        //第一步:在values文件夹下创建attrs.xml
        //第二步:详见attrs.xml文件内部
        //第三步:在所在的布局文件的根layout中添加xmlns:app="http://schemas.android.com/apk/res-auto"
        //第四步:在布局文件的控件中添加app:"你在attrs中设置的attr name"="你的值"
        //第五步:调用下面这句话,最后的为R.styleable.你在attrs中设置的declare-styleable name
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTurntableView);
        //第六步:调用下面这句话,根据你在attrs中设置的format,选择getXXX方法,
        //入参为 R.styleable. 加上 你在attrs中设置的declare-styleable name 加上 _ 加上 你在attrs中设置的attr name
        mStr = typedArray.getString(R.styleable.CustomTurntableView_text);
        init();
    }

    private void init() {
        //以下注释请看CustomBingView里面
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(10);
        mPaint.setTextSize(60);
        mPaint.setStyle(Paint.Style.FILL);

        mRectF = new RectF();
        mRectF.top = 100;
        mRectF.bottom = 400;
        mRectF.right = 550;
        mRectF.left = 350;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//        setMeasuredDimension(300, 300);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //设置画笔颜色为黑色,
        mPaint.setColor(Color.BLACK);
        //画出指针,用一个扇形,然后盖住后面补分来简单表示
        canvas.drawArc(mRectF, 60, 60, true, mPaint);

        mPaint.setColor(Color.RED);
        //画一个红色的圆形,就是中间的大按钮
        canvas.drawCircle(450, 450, 150, mPaint);

        mPaint.setColor(Color.BLACK);
        //添加按钮上的文字
        canvas.drawText(mStr, 400, 450, mPaint);

        //画三角,第一步,创建路径
//        Path path = new Path();
        //第二步,moveTo第一个顶点
//        path.moveTo(300, 300);
        //后续相继lineTo其他顶点
//        path.lineTo(300, 400);
//        path.lineTo(400, 400);
        //闭合
//        path.close();
//      画
//        canvas.drawPath(path, mPaint);
    }
}
/
package com.bawei.myapplication.turntable;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 这里是画转盘的
 *  @author hasee
 */
public class CustomTurntableView extends View{
    Paint mPaint;
    int mCircleCount = 6;
    float mStartAngle = 0;

    RectF rectF;

    public CustomTurntableView(Context context) {
        super(context);
        init();
    }

    public CustomTurntableView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(10);
        mPaint.setTextSize(60);
        mPaint.setStyle(Paint.Style.FILL);

        rectF = new RectF();
        rectF.top = 100;
        rectF.left = 100;
        rectF.right = 800;
        rectF.bottom = 800;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for(int i = 0; i  <  mCircleCount; i++){
            //按角标单双号设置扇形颜色,
            if(i % 2 == 0 ){
                mPaint.setColor(Color.BLUE);
            }else{
                mPaint.setColor(Color.GREEN);
            }
            canvas.drawArc(rectF, mStartAngle, 60, true, mPaint);

            mStartAngle += 60;
        }
    }
}

package com.bawei.myapplication.turntable;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.RotateAnimation;

import com.bawei.myapplication.R;
import com.bawei.myapplication.turntable.CustomTurntableView;

/**
 * 转盘
 * @author hasee
 */
public class TurntableActivity extends AppCompatActivity {

    CustomTurntableView customTurntableView;
    boolean isTouchInSide = false;
    float mDownX, mDownY;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_turntable);

        initView();
    }

    private void initView() {
        customTurntableView = findViewById(R.id.custom);
//        findViewById(R.id.custom_inside).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                float degrees =  (float)(720 + Math.random() * 1000);
//                RotateAnimation rotateAnimation = new RotateAnimation(0, -degrees, 450, 450);
//                rotateAnimation.setDuration(5000);
//                rotateAnimation.setFillAfter(true);
//                customCircleView.startAnimation(rotateAnimation);
//            }
//        });

        findViewById(R.id.custom_inside).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN &&
                        event.getX() > 300 &&
                        event.getX() < 600 &&
                        event.getY() > 300 &&
                        event.getY() < 600) {
                    isTouchInSide = true;
                    mDownX = event.getX();
                    mDownY = event.getY();
                    return true;
                }else if(event.getAction() == MotionEvent.ACTION_MOVE && (
                        event.getX() < mDownX -10 ||
                        event.getX() > mDownX + 10 ||
                        event.getY() < mDownY -10 ||
                        event.getY() > mDownY + 10) ){
                    isTouchInSide = false;
                } else if (event.getAction() == MotionEvent.ACTION_UP &&
                        event.getX() > mDownX -10 &&
                        event.getX() < mDownX + 10 &&
                        event.getY() > mDownY -10 &&
                        event.getY() < mDownY + 10 &&
                        isTouchInSide) {
                    float degrees = (float) (720 + Math.random() * 1000);
                    RotateAnimation rotateAnimation = new RotateAnimation(0, -degrees, 450, 450);
                    rotateAnimation.setDuration(5000);
                    rotateAnimation.setFillAfter(true);
                    customTurntableView.startAnimation(rotateAnimation);
                }
                isTouchInSide = false;
                return false;
            }
        });
    }
}

valuesxia创建 attrs.xml文件


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- name为想要调用这个属性的类名即可 -->

    <declare-styleable name="CustomTurntableView">
        <!-- name为属性的名字,可以随意起,只要符合规则看得懂 -->
        <!-- format为属性内容的类型 -->
        <attr name="text" format="string"></attr>
    </declare-styleable>
</resources>

//流式布局

package com.bawei.myapplication.waterfall;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bawei.myapplication.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hasee
 */
public final class CustomWaterFallViewGroup extends LinearLayout{
    /**
     *   每行最大允许字符串商都
     */

    final int mMaxSize = 22;
    /**
     *     传入的字符串数组
      */
    List<String> stringList = new ArrayList<>();
    Context mContext;

    public CustomWaterFallViewGroup(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public CustomWaterFallViewGroup(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        // 设置最外层的LinearLayout为垂直布局
        setOrientation(VERTICAL);
    }


    public void setData(List<String> stringList){
        // 直接用新的列表,重新绘制
        this.stringList = stringList;
        showData();
    }

    public void showData(){
        //因为每一次都要新画,所以移除以前所有的布局
        removeAllViews();
        // 优先向根布局添加一条横向布局
        LinearLayout linearLayout_h = (LinearLayout) View.inflate(mContext, R.layout.item_water_fall_h, null);
        addView(linearLayout_h);

        // 定义临时变量,用来计算最后一行已有的字符串长度
        int len = 0;

        //遍历
        for(String str: stringList){
            // 将此字符串长度与记录的已有字符串长度相加
            len += str.length();
            // 如果长度大于规定最大长度,说明这一行放不下了,需要换行
            if(len > mMaxSize){
                // 向根布局添加一条横向布局
                linearLayout_h = (LinearLayout) View.inflate(mContext, R.layout.item_water_fall_h, null);
                addView(linearLayout_h);
                // 因为换行,所以这一个字符串长度就是最后一行长度
                len = str.length();
            }

            // 添加TextView,并赋值
            View view = View.inflate(mContext, R.layout.item_water_fall, null);
            TextView textView = view.findViewById(R.id.tv_item_water_fall);
            textView.setText(str);
            linearLayout_h.addView(view);

            // 设置权重,让每一行内所有控件相加充满整行,并合理分配
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)view.getLayoutParams();
            layoutParams.weight = 1;
            view.setLayoutParams(layoutParams);
        }
    }
}


package com.bawei.myapplication.waterfall;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.bawei.myapplication.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 瀑布流
 * @author hasee
 */
public class WaterFallActivity extends AppCompatActivity {
    List<String> strList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_water_fall);
        initView();
    }

    private void initView() {
        final EditText editText = findViewById(R.id.edit);
        final CustomWaterFallViewGroup customWaterFallViewGroup = findViewById(R.id.water_fall);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取输入框文字
                String str = editText.getText().toString();
                // 将文字放入列表
                strList.add(str);
                // 设置数据
                customWaterFallViewGroup.setData(strList);
            }
        });
    }
}


//搜索框

package com.bawei.myapplication.weektest;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.bawei.myapplication.R;

public class TitleBarView extends LinearLayout {
    Context mContext;

    public TitleBarView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public TitleBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        //添加布局
        View view = View.inflate(mContext, R.layout.title, null);
        final EditText editText = view.findViewById(R.id.edit_title);
        view.findViewById(R.id.search_title).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //第六步:在将要回调的地方,首先判断非空
                if(mOnButtonClickListener != null){
                    //第七步:执行回调方法,传参
                    mOnButtonClickListener.onButtonClick(editText.getText().toString());
                }
            }
        });
        addView(view);
    }

    //第三步:设置成员变量
    OnBuutonClickListener mOnButtonClickListener;

    //第四步:传入,并且给成员变量赋值
    //第五步:在想要接受回调的地方,调用set方法,设置监听,详见WeekTestActivty #Line31
    public void  setButtonClickListener(OnBuutonClickListener onBuutonClickListener){
        mOnButtonClickListener = onBuutonClickListener;
    }

    //第一步:定义一个接口
    public interface OnBuutonClickListener{
        //第二步:写好方法和回传参数
        void onButtonClick(String str);
    }
}
//
package com.bawei.myapplication.weektest;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bawei.myapplication.R;

public class WeekFlowLayout extends LinearLayout {
    /**
     * 孩子中最高的一个
     */
    private int mChildMaxHeight;

    /**
     * 每一个孩子的左右的间距
     * 20是默认值,单位是px
     */
    private int mHSpace = 20;

    /**
     * 每一行的上下的间距
     * 20是默认值,单位是px
     */
    private int mVSpace = 20;

    public WeekFlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 拿到父容器推荐的宽和高以及计算模式
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

        //测量孩子的大小,一定要写这个
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        //寻找孩子中最高的一个孩子,找到的值会放在mChildMaxHeight变量中
        findMaxChildMaxHeight();

        //初始化值
        int left = 0, top = 0;

        //循环所有的孩子
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            //是否是一行的开头
            if (left != 0) {
                //需要换行了,因为放不下啦
                if ((left + view.getMeasuredWidth()) > sizeWidth) {
                    //计算出下一行的top
                    top += mChildMaxHeight + mVSpace;
                    left = 0;
                }
            }
            left += view.getMeasuredWidth() + mHSpace;
        }
        setMeasuredDimension(sizeWidth, (top + mChildMaxHeight) > sizeHeight ? sizeHeight : top + mChildMaxHeight);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        findMaxChildMaxHeight();

        //开始安排孩子的位置

        //初始化值
        int left = 0, top = 0;

        //循环所有的孩子
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            //是否是一行的开头
            if (left != 0) {
                //需要换行了,因为放不下啦
                if ((left + view.getMeasuredWidth()) > getWidth()) {
                    //计算出下一行的top
                    top += mChildMaxHeight + mVSpace;
                    left = 0;
                }
            }

            //安排孩子的位置
            view.layout(left, top, left + view.getMeasuredWidth(), top + mChildMaxHeight);
            left += view.getMeasuredWidth() + mHSpace;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    /**
     * 寻找孩子中最高的一个孩子
     */
    private void findMaxChildMaxHeight() {
        mChildMaxHeight = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            if (view.getMeasuredHeight() > mChildMaxHeight) {
                mChildMaxHeight = view.getMeasuredHeight();
            }
        }
    }
}
///
package com.bawei.myapplication.weektest;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

import com.bawei.myapplication.R;

@SuppressLint("AppCompatCustomView")
public class WeekGroupNameLayout extends TextView{
    public WeekGroupNameLayout(Context context) {
        super(context);
    }

    public WeekGroupNameLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        //自定义属性
        //第一步,在value文件夹下建立一个attr.xml文件,
        //第二步,写<declear....标签
        //第三步,写<attr 标签 name:方法名 format:属性
        //第四步,在布局文件根控件中写xmlns:app="http://schemas.android.com/apk/res-auto"
        //第五步,在想要调用自定义属性的控件中添加app:方法名=“想要设置的值”
        //第六步,在自定义view中的有AttributeSet的构造方法里写以下代码
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WeekFlowLayout);
        int color = typedArray.getColor(R.styleable.WeekFlowLayout_textColor, Color.BLACK);

        setTextColor(color);

        //最后要回收
        typedArray.recycle();
    }


}
/
package com.bawei.myapplication.weektest;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bawei.myapplication.R;

import java.util.UUID;

public class WeekTestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_week_test);

        init();
    }

    private void init(){
        final WeekFlowLayout fl_search = findViewById(R.id.fl_search);
        WeekFlowLayout fl_hot = findViewById(R.id.fl_hot);

        TitleBarView title = findViewById(R.id.title);
        title.setButtonClickListener(new TitleBarView.OnBuutonClickListener() {
            @Override
            public void onButtonClick(final String str) {
                // 随机字符串,当作唯一标示
                UUID uuid = UUID.randomUUID();
                Log.i("dj", "创建时的UUID:" + uuid + " ,字符串是:" + str);
                TextView tv = new TextView(WeekTestActivity.this);
                tv.setTag(uuid);
                tv.setTextColor(Color.RED);
                tv.setText(str);
                tv.setBackgroundResource(R.drawable.edit_bg);
                fl_search.addView(tv);

                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String uuid = String.valueOf(v.getTag());
                        Log.i("dj", "点击时的UUID:" + uuid + " ,字符串是:" + str);
                    }
                });
            }
        });

        for(int i = 0; i < 30; i++){
            TextView tv = new TextView(WeekTestActivity.this);
            tv.setText("数据 " + i);
            tv.setTextColor(Color.RED);
            tv.setBackgroundResource(R.drawable.edit_bg);
            fl_hot.addView(tv);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值