现在的位置: 首页 > 综合 > 正文

android弹出单选、多选菜单

2018年02月13日 ⁄ 综合 ⁄ 共 4943字 ⁄ 字号 评论关闭

菜单选择窗口:

 

 

菜单多选窗口

 

菜单单选窗口:

 

 

[java] view
plain
copy

  1. import android.app.Activity;  
  2. import android.app.AlertDialog;  
  3. import android.content.DialogInterface;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.ListView;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends Activity {  
  12.  private String[] areas = new String[]{"全部","玉兰香苑""张江地铁站""金科路""张江路""紫薇路""香楠小区" };  
  13.  private boolean[] areaState=new boolean[]{truefalsefalsefalsefalsefalse,false };  
  14.  private RadioOnClick radioOnClick = new RadioOnClick(1);  
  15.  private ListView areaCheckListView;  
  16.  private ListView areaRadioListView;  
  17.    private Button alertButton;  
  18.    private Button checkBoxButton;  
  19.    private Button radioButton;  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.           
  25.         alertButton=(Button)findViewById(R.id.alertButton);  
  26.         checkBoxButton=(Button)findViewById(R.id.checkBoxButton);  
  27.         radioButton=(Button)findViewById(R.id.radioButton);  
  28.           
  29.         alertButton.setOnClickListener(new AlertClickListener());  
  30.         checkBoxButton.setOnClickListener(new CheckBoxClickListener());  
  31.         radioButton.setOnClickListener(new RadioClickListener());  
  32.     }  
  33.     /** 
  34.      * 菜单弹出窗口 
  35.      * @author xmz 
  36.      * 
  37.      */  
  38.     class AlertClickListener implements OnClickListener{  
  39.   @Override  
  40.   public void onClick(View v) {  
  41.    new AlertDialog.Builder(MainActivity.this).setTitle("选择区域").setItems(areas,new DialogInterface.OnClickListener(){  
  42.       public void onClick(DialogInterface dialog, int which){  
  43.        Toast.makeText(MainActivity.this"您已经选择了: " + which + ":" + areas[which],Toast.LENGTH_LONG).show();  
  44.        dialog.dismiss();  
  45.       }  
  46.    }).show();  
  47.   }  
  48.     }  
  49.     /** 
  50.      * 多选框弹出菜单窗口 
  51.      * @author xmz 
  52.      * 
  53.      */  
  54.     class CheckBoxClickListener implements OnClickListener{  
  55.   @Override  
  56.   public void onClick(View v) {  
  57.    AlertDialog ad = new AlertDialog.Builder(MainActivity.this)  
  58.    .setTitle("选择区域")  
  59.    .setMultiChoiceItems(areas,areaState,new DialogInterface.OnMultiChoiceClickListener(){  
  60.       public void onClick(DialogInterface dialog,int whichButton, boolean isChecked){  
  61.        //点击某个区域  
  62.       }  
  63.      }).setPositiveButton("确定",new DialogInterface.OnClickListener(){  
  64.       public void onClick(DialogInterface dialog,int whichButton){  
  65.        String s = "您选择了:";  
  66.        for (int i = 0; i < areas.length; i++){  
  67.         if (areaCheckListView.getCheckedItemPositions().get(i)){  
  68.          s += i + ":"+ areaCheckListView.getAdapter().getItem(i)+ "  ";  
  69.         }else{  
  70.          areaCheckListView.getCheckedItemPositions().get(i,false);  
  71.         }  
  72.        }  
  73.        if (areaCheckListView.getCheckedItemPositions().size() > 0){  
  74.         Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();  
  75.        }else{  
  76.          //没有选择  
  77.        }  
  78.        dialog.dismiss();  
  79.       }  
  80.      }).setNegativeButton("取消"null).create();  
  81.    areaCheckListView = ad.getListView();  
  82.    ad.show();  
  83.   }  
  84.     }  
  85.       
  86.     /** 
  87.      * 单选弹出菜单窗口 
  88.      * @author xmz 
  89.      * 
  90.      */  
  91.     class RadioClickListener implements OnClickListener {  
  92.   @Override  
  93.   public void onClick(View v) {  
  94.    AlertDialog ad =new AlertDialog.Builder(MainActivity.this).setTitle("选择区域")  
  95.    .setSingleChoiceItems(areas,radioOnClick.getIndex(),radioOnClick).create();  
  96.    areaRadioListView=ad.getListView();  
  97.    ad.show();  
  98.   }  
  99.     }  
  100.     /** 
  101.      * 点击单选框事件 
  102.      * @author xmz 
  103.      * 
  104.      */  
  105.     class RadioOnClick implements DialogInterface.OnClickListener{  
  106.   private int index;  
  107.   
  108.   public RadioOnClick(int index){  
  109.    this.index = index;  
  110.   }  
  111.   public void setIndex(int index){  
  112.    this.index=index;  
  113.   }  
  114.   public int getIndex(){  
  115.    return index;  
  116.   }  
  117.   
  118.   public void onClick(DialogInterface dialog, int whichButton){  
  119.     setIndex(whichButton);  
  120.     Toast.makeText(MainActivity.this"您已经选择了: " + index + ":" + areas[index], Toast.LENGTH_LONG).show();  
  121.     dialog.dismiss();  
  122.   }  
  123.  }  
  124. }  

 

main.xml代码:

 

[java] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <Button  
  8.     android:id="@+id/alertButton"  
  9.     android:layout_width="wrap_content"  
  10.     android:layout_height="wrap_content"  
  11.     android:text="菜单选择窗口"  
  12. />      
  13. <Button  
  14.     android:id="@+id/checkBoxButton"  
  15.     android:layout_width="wrap_content"  
  16.     android:layout_height="wrap_content"  
  17.     android:text="多选菜单选择窗口"  
  18. />  
  19. <Button  
  20.     android:id="@+id/radioButton"  
  21.     android:layout_width="wrap_content"  
  22.     android:layout_height="wrap_content"  
  23.     android:text="单选菜单选择窗口-1"  
  24. />  
  25. </LinearLayout>  

转自:http://blog.csdn.net/meizhen51/article/details/6155912

抱歉!评论已关闭.