![](/img/article/atype.png)
制作一个简单通用的弹出选择框
LinearLayout ll =(LinearLayout)getActivity().findViewById(R.id.log_sel_qyport); ll.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ShowChoise(); } });
private void ShowChoise() { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Holo_Light_Dialog); //builder.setIcon(R.drawable.ic_launcher); builder.setTitle("选择一个城市"); // 指定下拉列表的显示数据 final String[] cities = {"广州", "上海", "北京", "香港", "澳门"}; // 设置一个下拉的列表选择项 builder.setItems(cities, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getActivity(), "选择的城市为:" + cities[which], Toast.LENGTH_SHORT).show(); } }); builder.show(); }
android 弹出选择框默认是显示的是黑底白字,如果要改为白底黑字则实例化的时候指定下风格
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Holo_Light_Dialog);
但是修改成白底黑字后可能会出现多出来背景的问题可以使用把背景设置成透明色解决
方法1: r_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
AlertDialog r_dialog = builder.create(); r_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); r_dialog.show();
方法2:重写写样式
<style name="MyDialog" parent="android:Theme.Holo.Light.Dialog"> <item name="android:windowBackground">@android:color/transparent</item> </style>
简单通用弹出选择框,keys没有可以不传
public void Choise(final List<String> values,final List<String> keys,final FragmentActivity _ac,final DoubleAction _action,String _title) { if(values.size()==0) { Toast.makeText(_ac, "正好努力数据中,请稍等", Toast.LENGTH_SHORT).show(); } AlertDialog.Builder builder = new AlertDialog.Builder(_ac,R.style.MyDialog); //builder.setIcon(R.drawable.ic_launcher); builder.setTitle(_title); // 指定下拉列表的显示数据 // 设置一个下拉的列表选择项 builder.setItems(values.toArray(new String[values.size()]), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String name = values.get(which); if(name.equals("未选择")) { _action.actionDouble(name,"-1"); } else { if(keys==null) { _action.actionDouble(name,null); } else { _action.actionDouble(name,keys.get(which)); } } } }); AlertDialog r_dialog = builder.create(); r_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); r_dialog.show(); }
写好接口
public interface DoubleAction { public void actionDouble(String _value,String _key); }
方法
private void addChoiseEvent() { final List<String> values = new ArrayList<String>(); values.add("a船"); values.add("s船"); final List<String> keys = new ArrayList<String>(); keys.add("1"); keys.add("2"); final TextView lsm =(TextView)getActivity().findViewById(R.id.manual_shipname); lsm.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { new SimpleChoise().Choise(values,keys,getActivity(),new DoubleAction(){ @Override public void actionDouble(String _value, String _key) { // TODO Auto-generated method stub lsm.setText("value:"+_value+",key:"+_key); //lsm.setTag(data2); } },"请选择船舶"); } }); }
欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739
评价