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

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布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/edittext"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:hint="请输入商品"/>
        <Button
            android:id="@+id/serach"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="搜索"/>
    </LinearLayout>
    <com.zhy.view.flowlayout.FlowLayout
        android:id="@+id/flowlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

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<String> 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);
                }
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/gy1115/article/details/86550726