android搜索框实现

依赖

compile 'com.hyman:flowlayout-lib:1.1.2'

页面布局 



    
        
        

主要代码实现 

public class MainActivity extends AppCompatActivity {

    private FlowLayout flow;
    private EditText edit;
    private ArrayList string;
    private Button search;
    private LinearLayout.LayoutParams layoutParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        flow = findViewById(R.id.flowlayout);
        edit = findViewById(R.id.edittext);
        search = findViewById(R.id.serach);

        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 5, 10, 5);
        //点击搜索按钮获取数据在流式布局中显示
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String messages = edit.getText().toString();
                string = new ArrayList<>();
                string.add(messages);
                for (int i = 0; i < string.size(); i++) {
                    TextView tv = new TextView(MainActivity.this);
                    tv.setPadding(28, 10, 28, 10);
                    tv.setText(string.get(i));
                    tv.setMaxEms(10);
                    tv.setSingleLine();
                    tv.setLayoutParams(layoutParams);
                    flow.addView(tv, layoutParams);
                }
            }
        });

    }
}

 

你可能感兴趣的:(android搜索框实现)