android 弹出选择框,简单通用弹出选择框



[java]  view plain  copy
  1. LinearLayout ll =(LinearLayout)getActivity().findViewById(R.id.log_sel_qyport);  
  2.         ll.setOnClickListener(new View.OnClickListener() {  
  3.   
  4.                 public void onClick(View v) {  
  5.                     ShowChoise();         
  6.                 }  
  7.         });  

[java]  view plain  copy
  1. private void ShowChoise()  
  2. {  
  3.       
  4.     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Holo_Light_Dialog);  
  5.        //builder.setIcon(R.drawable.ic_launcher);  
  6.        builder.setTitle("选择一个城市");  
  7.        //    指定下拉列表的显示数据  
  8.        final String[] cities = {"广州""上海""北京""香港""澳门"};  
  9.        //    设置一个下拉的列表选择项  
  10.        builder.setItems(cities, new DialogInterface.OnClickListener()  
  11.        {  
  12.            @Override  
  13.            public void onClick(DialogInterface dialog, int which)  
  14.            {  
  15.                Toast.makeText(getActivity(), "选择的城市为:" + cities[which], Toast.LENGTH_SHORT).show();  
  16.            }  
  17.        });  
  18.        builder.show();  
  19. }  


Android 弹出选择框默认是显示的是黑底白字,如果要改为白底黑字则实例化的时候指定下风格

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Holo_Light_Dialog);


但是修改成白底黑字后可能会出现多出来背景的问题可以使用把背景设置成透明色解决

方法1:   r_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

[java]  view plain  copy
  1. AlertDialog r_dialog = builder.create();  
  2.        r_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);  
  3.        r_dialog.show();  
方法2:重写写样式

[html]  view plain  copy
  1. <style name="MyDialog"  
  2.          parent="android:Theme.Holo.Light.Dialog">   
  3.            <item name="android:windowBackground">@android:color/transparent</item>                               
  4.      </style>   



简单通用弹出选择框,keys没有可以不传


[java]  view plain  copy
  1. public void Choise(final List<String> values,final List<String> keys,final FragmentActivity _ac,final DoubleAction _action,String _title)  
  2.     {         
  3.         if(values.size()==0)  
  4.         {  
  5.             Toast.makeText(_ac, "正好努力数据中,请稍等", Toast.LENGTH_SHORT).show();  
  6.         }         
  7.         AlertDialog.Builder builder = new AlertDialog.Builder(_ac,R.style.MyDialog);  
  8.         //builder.setIcon(R.drawable.ic_launcher);  
  9.         builder.setTitle(_title);  
  10.         //    指定下拉列表的显示数据  
  11.         //    设置一个下拉的列表选择项  
  12.         builder.setItems(values.toArray(new String[values.size()]), new DialogInterface.OnClickListener()  
  13.         {  
  14.             @Override  
  15.             public void onClick(DialogInterface dialog, int which)  
  16.             {     
  17.                 String name = values.get(which);  
  18.                 if(name.equals("未选择"))  
  19.                 {  
  20.                     _action.actionDouble(name,"-1");  
  21.                 }  
  22.                 else  
  23.                 {  
  24.                     if(keys==null)  
  25.                     {  
  26.                         _action.actionDouble(name,null);  
  27.                     }  
  28.                     else  
  29.                     {  
  30.                       _action.actionDouble(name,keys.get(which));  
  31.                     }  
  32.                 }  
  33.             }  
  34.         });  
  35.           
  36.         AlertDialog r_dialog = builder.create();  
  37.         r_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);  
  38.         r_dialog.show();  
  39.     }  

[java]  view plain  copy
  1. public interface DoubleAction {  
  2.   
  3.     public void actionDouble(String _value,String _key);  
  4. }  
[java]  view plain  copy
  1. private void addChoiseEvent()  
  2.    {  
  3.     final List<String> values = new ArrayList<String>();  
  4.     values.add("a船");  
  5.     values.add("s船");  
  6.       
  7.     final List<String> keys = new ArrayList<String>();  
  8.     keys.add("1");  
  9.     keys.add("2");  
  10.       
  11.     final TextView lsm =(TextView)getActivity().findViewById(R.id.manual_shipname);           
  12.     lsm.setOnClickListener(new View.OnClickListener() {  
  13.         public void onClick(View v) {  
  14.             new SimpleChoise().Choise(values,keys,getActivity(),new DoubleAction(){  
  15.                 @Override  
  16.                 public void actionDouble(String _value, String _key) {  
  17.                     // TODO Auto-generated method stub  
  18.                     lsm.setText("value:"+_value+",key:"+_key);  
  19.                     //lsm.setTag(data2);  
  20.                 }     
  21.             },"请选择船舶");  
  22.         }  
  23.     });  
  24.    }  
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页