1. 程式人生 > >android彈出下拉選擇選單,單選,多選

android彈出下拉選擇選單,單選,多選

選單選擇視窗:

選單多選視窗

選單單選視窗:

[java] view plaincopyprint?
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public
class MainActivity extends Activity { private String[] areas = new String[]{"全部","玉蘭香苑", "張江地鐵站", "金科路", "張江路", "紫薇路", "香楠小區" }; private boolean[] areaState=new boolean[]{true, false, false, false, false, false,false }; private RadioOnClick radioOnClick = new RadioOnClick(1); private ListView areaCheckListView; private
ListView areaRadioListView; private Button alertButton; private Button checkBoxButton; private Button radioButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); alertButton=(Button)findViewById(R.id.alertButton); checkBoxButton=(Button)findViewById(R.id.checkBoxButton); radioButton=(Button)findViewById(R.id.radioButton); alertButton.setOnClickListener(new
AlertClickListener()); checkBoxButton.setOnClickListener(new CheckBoxClickListener()); radioButton.setOnClickListener(new RadioClickListener()); } /** * 選單彈出視窗 * @author xmz * */ class AlertClickListener implements OnClickListener{ @Override public void onClick(View v) { new AlertDialog.Builder(MainActivity.this).setTitle("選擇區域").setItems(areas,new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which){ Toast.makeText(MainActivity.this, "您已經選擇了: " + which + ":" + areas[which],Toast.LENGTH_LONG).show(); dialog.dismiss(); } }).show(); } } /** * 多選框彈出選單視窗 * @author xmz * */ class CheckBoxClickListener implements OnClickListener{ @Override public void onClick(View v) { AlertDialog ad = new AlertDialog.Builder(MainActivity.this) .setTitle("選擇區域") .setMultiChoiceItems(areas,areaState,new DialogInterface.OnMultiChoiceClickListener(){ public void onClick(DialogInterface dialog,int whichButton, boolean isChecked){ //點選某個區域 } }).setPositiveButton("確定",new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int whichButton){ String s = "您選擇了:"; for (int i = 0; i < areas.length; i++){ if (areaCheckListView.getCheckedItemPositions().get(i)){ s += i + ":"+ areaCheckListView.getAdapter().getItem(i)+ " "; }else{ areaCheckListView.getCheckedItemPositions().get(i,false); } } if (areaCheckListView.getCheckedItemPositions().size() > 0){ Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show(); }else{ //沒有選擇 } dialog.dismiss(); } }).setNegativeButton("取消", null).create(); areaCheckListView = ad.getListView(); ad.show(); } } /** * 單選彈出選單視窗 * @author xmz * */ class RadioClickListener implements OnClickListener { @Override public void onClick(View v) { AlertDialog ad =new AlertDialog.Builder(MainActivity.this).setTitle("選擇區域") .setSingleChoiceItems(areas,radioOnClick.getIndex(),radioOnClick).create(); areaRadioListView=ad.getListView(); ad.show(); } } /** * 點選單選框事件 * @author xmz * */ class RadioOnClick implements DialogInterface.OnClickListener{ private int index; public RadioOnClick(int index){ this.index = index; } public void setIndex(int index){ this.index=index; } public int getIndex(){ return index; } public void onClick(DialogInterface dialog, int whichButton){ setIndex(whichButton); Toast.makeText(MainActivity.this, "您已經選擇了: " + index + ":" + areas[index], Toast.LENGTH_LONG).show(); dialog.dismiss(); } } } main.xml程式碼: [java] view plaincopyprint? <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/alertButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="選單選擇視窗" /> <Button android:id="@+id/checkBoxButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多選選單選擇視窗" /> <Button android:id="@+id/radioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="單選選單選擇視窗-1" /> </LinearLayout>