Android的List Dialog实例

1 按钮单击事件的主要内容

final CharSequence[] charSequences = {"aa","bb","cc"};
			AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
			
			builder.setTitle("ok")
					.setIcon(R.drawable.ic_launcher)
					.setItems(charSequences, new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Toast.makeText(MainActivity.this, charSequences[which],Toast.LENGTH_SHORT).show();
							Log.i("abc", "i"+which);
							
						}
					}).show();



2 结果图


3 完整代码


package com.example.a;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	public static final int SAVE = Menu.FIRST;
	public static final int RETURN_MENU = Menu.FIRST + 1;
	public static final int QUIT = Menu.FIRST + 2;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ImageButton btnButton=(ImageButton)this.findViewById(R.id.btn);
		btnButton.setOnClickListener(new MyListener());
		//此处长按是不起作用的
//		btnButton.setOnLongClickListener(new MylongClick());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, SAVE, 0, "保存");
		menu.add(0, RETURN_MENU, 1, "返回主菜单");
		menu.add(0, QUIT, 2, "退出");
		return super.onCreateOptionsMenu(menu);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		TextView tv = (TextView) findViewById(R.id.txt);
		switch (item.getItemId()) {
		case SAVE:
			tv.setText("保存按钮被点击");
			break;
		case RETURN_MENU:
			tv.setText("返回主菜单按钮被点击");
			break;
		case QUIT:
			tv.setText("退出按钮被点击");
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private class MyListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			
			final CharSequence[] charSequences = {"aa","bb","cc"};
			AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
			
			builder.setTitle("ok")
					.setIcon(R.drawable.ic_launcher)
					.setItems(charSequences, new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Toast.makeText(MainActivity.this, charSequences[which],Toast.LENGTH_SHORT).show();
							Log.i("abc", "i"+which);
							
						}
					}).show();
//			Dialog dia = new AlertDialog.Builder(MainActivity.this).setTitle("要干什么")
//					.setPositiveButton("我再想想", new DialogInterface.OnClickListener() {
//						
//						@Override
//						public void onClick(DialogInterface dialog, int which) {
//							Toast.makeText(MainActivity.this, "我再想呢", 1);
//						}
//					}).setNegativeButton("要不退出吧", new DialogInterface.OnClickListener() {
//						
//						@Override
//						public void onClick(DialogInterface dialog, int which) {
//							Toast.makeText(MainActivity.this, "haha", 1);
//							MainActivity.this.finish();
//						}
//					}).show();
//			
		}
	}
	
	private class MylongClick implements OnLongClickListener
	{
		@Override
		public boolean onLongClick(View v) {
			Toast.makeText(MainActivity.this, "我是长按住出来的", 1);
			Log.i("abc", "abc");
			return false;
		}
		
	}
	
}


你可能感兴趣的:(Android)