流失布局-键盘

1.自定义布局

package com.example.administrator.ydwlxcpt.Utils;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

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

/**
 * Created by Administrator on 2018/5/8.
 */
public class FlowGroupView extends ViewGroup {
    //存储所有子View
    private List<List<View>> mAllChildViews = new ArrayList<>();
    //每一行的高度
    private List<Integer> mLineHeight = new ArrayList<>();

    public FlowGroupView(Context context) {
        this(context, null);
        // TODO Auto-generated constructor stub
    }
    public FlowGroupView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        // TODO Auto-generated constructor stub
    }
    public FlowGroupView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        //父控件传进来的宽度和高度以及对应的测量模式
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        //如果当前ViewGroup的宽高为wrap_content的情况
        int width = 0;//自己测量的 宽度
        int height = 0;//自己测量的高度
        //记录每一行的宽度和高度
        int lineWidth = 0;
        int lineHeight = 0;

        //获取子view的个数
        int childCount = getChildCount();
        for(int i = 0;i < childCount; i ++){
            View child = getChildAt(i);
            //测量子View的宽和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            //得到LayoutParams
            MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
            //View占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            //View占据的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            //换行时候
            if(lineWidth + childWidth > sizeWidth){
                //对比得到最大的宽度
                width = Math.max(width, lineWidth);
                //重置lineWidth
                lineWidth = childWidth;
                //记录行高
                height += lineHeight;
                lineHeight = childHeight;
            }else{//不换行情况
                //叠加行宽
                lineWidth += childWidth;
                //得到最大行高
                lineHeight = Math.max(lineHeight, childHeight);
            }
            //处理最后一个子View的情况
            if(i == childCount -1){
                width = Math.max(width, lineWidth);
                height += lineHeight;
            }
        }
        //wrap_content
        setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width,
                modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        mAllChildViews.clear();
        mLineHeight.clear();
        //获取当前ViewGroup的宽度
        int width = getWidth();
        int lineWidth = 0;
        int lineHeight = 0;
        //记录当前行的view
        List<View> lineViews = new ArrayList<View>();
        int childCount = getChildCount();
        for(int i = 0;i < childCount; i ++){
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();

            //如果需要换行
            if(childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width){
                //记录LineHeight
                mLineHeight.add(lineHeight);
                //记录当前行的Views
                mAllChildViews.add(lineViews);
                //重置行的宽高
                lineWidth = 0;
                lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
                //重置view的集合
                lineViews = new ArrayList();
            }
            lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
            lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin);
            lineViews.add(child);
        }
        //处理最后一行
        mLineHeight.add(lineHeight);
        mAllChildViews.add(lineViews);

        //设置子View的位置
        int left = 0;
        int top = 0;
        //获取行数
        int lineCount = mAllChildViews.size();
        for(int i = 0; i < lineCount; i ++){
            //当前行的views和高度
            lineViews = mAllChildViews.get(i);
            lineHeight = mLineHeight.get(i);
            for(int j = 0; j < lineViews.size(); j ++){
                View child = lineViews.get(j);
                //判断是否显示
                if(child.getVisibility() == View.GONE){
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                int cLeft = left + lp.leftMargin;
                int cTop = top + lp.topMargin;
                int cRight = cLeft + child.getMeasuredWidth();
                int cBottom = cTop + child.getMeasuredHeight();
                //进行子View进行布局
                child.layout(cLeft, cTop, cRight, cBottom);
                left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            }
            left = 0;
            top += lineHeight;
        }

    }
    /**
     * 与当前ViewGroup对应的LayoutParams
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        // TODO Auto-generated method stub

        return new MarginLayoutParams(getContext(), attrs);
    }

}
2.Activity-Dialog

package com.example.administrator.ydwlxcpt.Activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.ydwlxcpt.R;
import com.example.administrator.ydwlxcpt.Utils.FlowGroupView;
import com.example.administrator.ydwlxcpt.View.FirstEvent;
import com.example.administrator.ydwlxcpt.View.TwoEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/8.
 */

public class YanSeActivity extends Activity {
    private String mNames[] = {
            "", "", "", "", "", "", "","绿",""
    };

    private FlowGroupView mFlowLayout,mFlowLayout_shuzi;
    private EditText chepai;
    private List<TextView> tvs = null;
    private TextView quxiao,queding;
    private ImageView shanchu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pop);
        initChildViews();
        setFinishOnTouchOutside(true);
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.height = (int) (d.getHeight() * 0.5); // 高度设置为屏幕的0.3
        p.width = (int) (d.getWidth() * 1); // 宽度设置为屏幕的0.7
        getWindow().setAttributes(p);
        p.gravity = Gravity.BOTTOM;//设置对话框置顶显示
        intview();
    }

    private void intview() {
        shanchu=(ImageView)findViewById(R.id.iv_yanse_shanchu);
        quxiao=(TextView)findViewById(R.id.tv_yanse_quxiao);
        queding=(TextView)findViewById(R.id.tv_yanse_queding);
        chepai=(EditText)findViewById(R.id.et_yanse_chepai);
        shanchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s=chepai.getText().toString();
                if(s.length()==0){
//判断字符长度,如果为0就不做任何处理
                }else if(s.length()>0){
                    chepai.setText(s.substring(0,s.length()-1));//将字符取的地方减一位需要减2.“sss”.length()=3,ss需要取的位置到1,因为subString是左闭右开,所以是需要-2
                }
            }
        });
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               String yanse= chepai.getText().toString();
                EventBus.getDefault().post(
                        new TwoEvent(yanse));
                finish();

            }
        });
    }

    private void initChildViews() {
        tvs = new LinkedList<TextView>();
        mFlowLayout = (FlowGroupView) findViewById(R.id.flowlayout_yanse);
        chepai = (EditText) findViewById(R.id.et_chepai);
        final ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.width=100;
        lp.height=100;
        lp.leftMargin = 10;
        lp.rightMargin = 10;
        lp.topMargin = 10;
        lp.bottomMargin = 10;
        for (int i = 0; i < mNames.length; i++) {
            final TextView view = new TextView(this);
            view.setText(mNames[i]);
            view.setTextColor(Color.BLACK);
            view.setTextSize(25);
            view.setpinding(10,0,10,0)
            view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg));
            tvs.add(view);
            mFlowLayout.addView(view, lp);
            final String str=view.getText().toString();
            tvs.get(i).setOnClickListener(new View.OnClickListener() {
                @Override

                public void onClick(View view) {
                    chepai.setText(str);
                    chepai.setCursorVisible(false);
                }
            });
        }
    }

}

3.页面布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="50dp"        android:background="@color/titlebg"        >        <TextView            android:id="@+id/tv_quxiao"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="取消"            android:layout_marginLeft="20dp"            android:layout_gravity="center_vertical"            android:textSize="20sp"            android:textColor="@color/white"/>        <TextView            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="键盘输入"            android:layout_gravity="center_vertical"            android:textSize="20sp"            android:textColor="@color/white"            android:layout_marginRight="40dp"            />        <TextView            android:id="@+id/tv_queding"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="确定"            android:layout_gravity="center_vertical"            android:textSize="20sp"            android:layout_marginRight="20dp"            android:textColor="@color/white"/>    </LinearLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">    <EditText        android:id="@+id/et_chepai"        android:layout_width="match_parent"        android:layout_height="50dp"        android:background="@drawable/edit_bg"        android:maxLength="7"        />        <ImageView            android:id="@+id/iv_shanchu"            android:layout_width="60dp"            android:layout_height="50dp"            android:src="@drawable/shanchu"            android:layout_centerVertical="true"            android:layout_alignParentRight="true"            android:layout_marginRight="20dp"            android:layout_alignRight="@+id/et_chepai"            />    </RelativeLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#ffffff"        >    <com.example.administrator.ydwlxcpt.Utils.FlowGroupView        android:id="@+id/flowlayout"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginTop="10dp"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        >    </com.example.administrator.ydwlxcpt.Utils.FlowGroupView>        <com.example.administrator.ydwlxcpt.Utils.FlowGroupView            android:id="@+id/flowlayout_shuzi"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_marginTop="10dp"            android:layout_marginLeft="20dp"            android:layout_marginRight="20dp"            >        </com.example.administrator.ydwlxcpt.Utils.FlowGroupView>    </RelativeLayout></LinearLayout>4.注册 EventBus  EventBus.getDefault().register(this);5.解绑 EventBus public void onDestroy() {        super.onDestroy();        EventBus.getDefault().unregister(this);//反注册EventBus    }6.接受参数  @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)    public void onEvent(FirstEvent event) {        String msg = event.getMsg();        Log.d("harvic", msg);        et_chepaihao.setText(msg);    }7.接口public class FirstEvent {    private String chepai;    public FirstEvent(String chepaihao) {    chepai=chepaihao;    }    public String getMsg(){        return chepai;    }}



猜你喜欢

转载自blog.csdn.net/ydc0128/article/details/80403547