Android 几种常用的Dialog的简单使用

为这几种dialog设置button按钮,通过触发button按钮来显示dialog对话框,MainActivity的具体代码如下:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViewById(R.id.btn01).setOnClickListener(new OnClickListener() {

			private AlertDialog dialog;

			@Override
			public void onClick(View v) {
				//显示一个普通对话框
				Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("普通对话框");
				builder.setIcon(R.drawable.ic_launcher);
				builder.setMessage("GD");
			    //在外层点击无反应,只能点击按钮取消
				builder.setCancelable(false);
				//设置左边的按钮,第一个参数是按钮显示的文字,第二个参数是该按钮的监听器
				builder.setNegativeButton("OK",new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
			
				//设置中间按钮
				builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
					    dialog.dismiss();
					}
				});
				
				//设置右边的按钮
				builder.setPositiveButton("右边", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
				builder.show();
				dialog = builder.create();
	
			}
		});

		
		
		findViewById(R.id.btn02).setOnClickListener(new OnClickListener() {
			
			//列表对话框,特有的setItems
			@Override
			public void onClick(View v) {
            //列表对话框
		    new AlertDialog.Builder(MainActivity.this)
		    .setIcon(R.drawable.ic_launcher)
		    .setTitle("列表对话框")
		    .setItems(R.array.array, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			}).create().show();
			}
		});
		
		
		findViewById(R.id.btn03).setOnClickListener(new OnClickListener() {
			
			private AlertDialog dialog;

			@Override
			public void onClick(View v) {
            //自定义的Dialog
			View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);
			Builder builder=new AlertDialog.Builder(MainActivity.this);
			view.findViewById(R.id.btn01).setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
				Toast.makeText(MainActivity.this, "big", Toast.LENGTH_SHORT).show();	
			    dialog.dismiss();
				}
			});
			builder.setView(view);
			dialog = builder.create();
			dialog.show();
			
			}
		});
		
		findViewById(R.id.btn04).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			//日期对话框
             Calendar calendar=Calendar.getInstance();
             DatePickerDialog dialog = new DatePickerDialog(MainActivity.this,new DatePickerDialog.OnDateSetListener() {
				
				@Override
				public void onDateSet(DatePicker view, int year, int monthOfYear,
						int dayOfMonth) {
					// TODO Auto-generated method stub
					
				}//Calendar.YEAR, Calendar.MONTH,Calendar.DAY_OF_MONTH
			}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
             dialog.show();
			}
			
		});
	}
}
activity_main.xml



    






你可能感兴趣的:(Android 几种常用的Dialog的简单使用)