欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android--下拉框

程序员文章站 2022-07-14 18:07:33
...
public class MyEdiSelect extends AppCompatActivity {

    private EditText et_main;
    private ImageView iv_main;
    private PopupWindow popupWindow;
    private ListView list;
    private  MyAdapter adapter;
    private String[] str={
            "one",
            "two",
            "three",
            "four"
    };
    private List<String> data;

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


        data=new ArrayList<String>();
        et_main=(EditText)findViewById(R.id.et_main);
        iv_main=(ImageView)findViewById(R.id.iv_main);


        setStr();
        setList();


        iv_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(popupWindow==null){
                    popupWindow=new PopupWindow(MyEdiSelect.this);
                    //下拉框宽度
                    popupWindow.setWidth(et_main.getWidth());
                    //下拉框的高度
                    popupWindow.setHeight(500);
                    //设置内容
                    popupWindow.setContentView(list);
                    popupWindow.setFocusable(true);//设置焦点
                }
                //下拉框在et_main下面,相距0单位
                popupWindow.showAsDropDown(et_main, 0, 0);
            }
        });


        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String msg=data.get(position);
                et_main.setText(msg);
                if(popupWindow!=null&&popupWindow.isShowing()){
                    popupWindow.dismiss();
                    popupWindow=null;
                }
            }
        });


    }

	//将数据加入list里面
    private void setStr() {
        for(int i=0;i<str.length;i++){
            data.add(str[i]);
        }
    }
    //设置适配器
    private void setList() {
        adapter=new MyAdapter();
        list=new ListView(MyEdiSelect.this);
        list.setAdapter(adapter);
    }

	//适配器
    public class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {
            ViewHolder viewHolder;

            if(convertView==null){
                convertView=View.inflate(MyEdiSelect.this, R.layout.base_item, null);
                viewHolder=new ViewHolder();
                viewHolder.textView=convertView.findViewById(R.id.tv_item);
                viewHolder.imageView=convertView.findViewById(R.id.iv_item);
                convertView.setTag(viewHolder);
            }else {
                viewHolder= (ViewHolder) convertView.getTag();
            }

            final String s=data.get(position);

            viewHolder.imageView.setImageResource(R.drawable.jd);
            viewHolder.textView.setText(s);




            viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    data.remove(position);
                    adapter.notifyDataSetChanged();
                }
            });
            return convertView;

        }
    }

    static class ViewHolder{
        TextView textView;
        ImageView imageView;
    }
}
相关标签: Android