Android开发笔记--Dialog提示框

对话框在应用程序中不是很必要的,不过好的对话框会给应用添加不少色彩,采用对话框可以增加应用的友好性。在这里我主要是通过几个Button呈现几个不同的对话框。Dialog类是所有对话框的基类,而他的生命周期是通过Activity来维护的。 
首先只是显示一个确认按钮的简单对话框,代码如下: 
Button dialog1 = (Button) findViewById(R.id.dialog1); 
dialog1.setOnClickListener(new OnClickListener() { 

public void onClick(View v) { 
AlertDialog.Builder builder = new Builder(DialogActivity.this); 
builder.setTitle("标题"); 
builder.setPositiveButton("确定",null); 
builder.setIcon(android.R.drawable.ic_dialog_info); 
builder.setMessage("简单消息框"); 
builder.show(); 
} 
}); 


下面是有确认取消的对话框 

Android开发笔记--Dialog提示框_第1张图片


Button dialog2 = (Button) findViewById(R.id.dialog2); 
dialog2.setOnClickListener(new OnClickListener() { 

public void onClick(View v) { 
AlertDialog.Builder builder = new Builder(DialogActivity.this); 
builder.setTitle("标题"); 
builder.setPositiveButton("确定",null); 
builder.setNegativeButton("取消",null); 
builder.setIcon(android.R.drawable.ic_dialog_info); 
builder.setMessage("简单消息框"); 
builder.show(); 
} 
}); 

Android开发笔记--Dialog提示框_第2张图片

下面是带有输入框的对话框 

Button dialog3 = (Button) findViewById(R.id.dialog3); 
dialog3.setOnClickListener(new OnClickListener() { 

public void onClick(View v) { 
AlertDialog.Builder builder = new Builder(DialogActivity.this); 
builder.setTitle("标题"); 
builder.setView(new EditText(DialogActivity.this)); 
builder.setPositiveButton("确定",null); 
builder.setNegativeButton("取消",null); 
builder.setIcon(android.R.drawable.ic_dialog_info); 
builder.setMessage("简单消息框"); 
builder.show(); 
} 
}); 

Android开发笔记--Dialog提示框_第3张图片

带有单选择按钮的对话框 

Button dialog4 = (Button) findViewById(R.id.dialog4); 
dialog4.setOnClickListener(new OnClickListener() { 

public void onClick(View v) {  
new AlertDialog.Builder(DialogActivity.this) 
.setTitle("请选择") 
.setIcon(android.R.drawable.ic_dialog_info) 
.setSingleChoiceItems(new String[] {"item1","item2","item3","item4"}, 0, 
    new DialogInterface.OnClickListener(){ 
        public void onClick(DialogInterface dialog, int which){ 
        dialog.dismiss(); 
        } 
    } 
) 
.setNegativeButton("取消", null) 
.show(); 
} 
}); 
Button dialog4 = (Button) findViewById(R.id.dialog4); 
dialog4.setOnClickListener(new OnClickListener() { 

public void onClick(View v) {  
new AlertDialog.Builder(DialogActivity.this) 
.setTitle("请选择") 
.setIcon(android.R.drawable.ic_dialog_info) 
.setSingleChoiceItems(new String[] {"item1","item2","item3","item4"}, 0, 
    new DialogInterface.OnClickListener(){ 
        public void onClick(DialogInterface dialog, int which){ 
        dialog.dismiss(); 
        } 
    } 
) 
.setNegativeButton("取消", null) 
.show(); 
} 
}); 

Android开发笔记--Dialog提示框_第4张图片

带有多选择按钮的对话框

Button dialog5 = (Button) findViewById(R.id.dialog5); 
dialog5.setOnClickListener(new OnClickListener() { 

public void onClick(View v) {  
new AlertDialog.Builder(DialogActivity.this) 
.setTitle("请选择") 
.setIcon(android.R.drawable.ic_dialog_info) 
.setMultiChoiceItems(new String[] {"item1","item2","item3","item4"}, null, null) 
.setNegativeButton("取消", null) 
.show(); 
} 
}); 

Android开发笔记--Dialog提示框_第5张图片

你可能感兴趣的:(Android开发笔记--Dialog提示框)