Dialog的使用(二):AlertDialog.setItems

通过AlertDialog.Builder的setItems方法可以创建简单的列表对话框。

package com.example.androidtest;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.util.Log;

public class MainActivity extends Activity {
	
	AlertDialog actions;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);
		setTitle("Activity");
		Button button = new Button(this);
		button.setText("Click for options");
		button.setOnClickListener(buttonListener);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Choose an options");
		String[] options = {"A", "B", "C"};
		builder.setItems(options, actionListener);
		builder.setNegativeButton("Cancel", null);
		actions = builder.create();
		
		setContentView(button);
	}	
	
	DialogInterface.OnClickListener actionListener = new DialogInterface.OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			switch (which) {
			case 0:
				break;
			case 1:
				break;
			case 2:
				break;
			default:
				break;
			}
		}
	};
	View.OnClickListener buttonListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			actions.show();
		}
	};
}


你可能感兴趣的:(Dialog的使用(二):AlertDialog.setItems)