点击搜索把搜索框里的内容展示倒流式布局

1.导入依赖

    implementation 'com.hyman:flowlayout-lib:1.1.2'
    implementation "com.jakewharton:butterknife:8.4.0"
    annotationProcessor "com.jakewharton:butterknife-compiler:8.4.0"

2.xml布局:



    
        
        

3.java代码:


public class MainActivity extends AppCompatActivity {

    @butterknife.BindView(R.id.edittext)
    EditText edittext;
    @butterknife.BindView(R.id.serach)
    Button serach;
    @butterknife.BindView(R.id.flowlayout)
    FlowLayout flowlayout;
    private LinearLayout.LayoutParams layoutParams;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        butterknife.ButterKnife.bind(this);
        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 5, 10, 5);
        //点击搜索按钮获取数据在流式布局中显示
        serach.setOnClickListener(new View.OnClickListener() {

            private ArrayList list;

            @Override
            public void onClick(View v) {
                //得到输入框的值
                String messages = edittext.getText().toString();
                list = new ArrayList<>();
                list.add(messages);
                for (int i = 0; i < list.size(); i++) {
                    TextView t = new TextView(MainActivity.this);
                    t.setPadding(28, 10, 28, 10);
                    t.setText(list.get(i));
                    t.setMaxEms(10);
                    t.setSingleLine();
                    t.setLayoutParams(layoutParams);
                    flowlayout.addView(t,layoutParams);
                }
            }
        });
    }
}

 

你可能感兴趣的:(分享代码区)