Android中Dialog的使用

来源:互联网 发布:数据库第五版答案 编辑:程序博客网 时间:2024/06/07 00:47
图一(提示对话框)Android中Dialog的使用图二(列表)Android中Dialog的使用

图三(多选)Android中Dialog的使用图四(单选)Android中Dialog的使用

Android中Dialog的使用
图一代码为showDialog1,图二为dialog2,图三为dialog3……
如何将自定义文件布局设置到弹出框中:
//实例化布局文件
final View view =getLayoutInflater().inflate(R.layout.land_layout,null);
builder5.setView(view)
上述代码完整模式
LayoutInflaterinflater = LayoutInflater.from(this);
View view =inflater.inflate(R.layout.radio_child);
builder5.setView(view)
代码区:
public void showDialog1(View v){
//创建一个提示对话框的构构造者
AlertDialog.Builderbuilder = newAlertDialog.Builder(this);
builder.setTitle("FirstDialog");
builder.setMessage("setMessage");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"GOGO",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"COMEBABY",Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("MID",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"I'mneutral",Toast.LENGTH_SHORT).show();
}
});
// AlertDialog dialog =builder.create();
//dialog.show();
builder.show();
}
public void dialog2(View v){
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setTitle("Please select a platform");
final String[] items = {"Android","ios","WindowPhone"};
builder2.setItems(items, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_SHORT).show();
}
});
AlertDialogalertDialog = builder2.create();
alertDialog.show();

}
public void dialog3(View v){
AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
builder3.setTitle("Please select a platform");
final String[] items = {"Android","ios","WindowPhone"};
final ArrayList list = new ArrayList();
builder3.setMultiChoiceItems(items,null, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int which,boolean isChecked) {
if (isChecked){
list.add(items[which]);
}else{
list.remove(items[which]);
}
}
});
builder3.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,list.toString(),Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder3.setNegativeButton("NO",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
list.clear();
dialog.dismiss();
}
});
AlertDialogalertDialog = builder3.create();
alertDialog.show();

}
String result = "android";
publicvoid dialog4(Viewv){
AlertDialog.Builder builder4 = new AlertDialog.Builder(this);
builder4.setTitle("Please select a platform");
final String[] items = {"Android","ios","WindowPhone"};

builder4.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result = items[which];
Toast.makeText(MainActivity.this,"YouSelected"+items[which],Toast.LENGTH_SHORT).show();
}
});
builder4.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, result,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder4.setNegativeButton("NO",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialogalertDialog = builder4.create();
alertDialog.show();

}
public void dialog5(View v){
AlertDialog.Builder builder5 = new AlertDialog.Builder(this);
builder5.setTitle("land");
builder5.setIcon(R.mipmap.ic_launcher);
// AlertDialog.Builder builder= builder5.setView(R.layout.land_layout);
//实例化布局文件
final View view =getLayoutInflater().inflate(R.layout.land_layout,null);
builder5.setView(view);
builder5.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText edit_username = (EditText) view.findViewById(R.id.editText1);
EditText edit_password = (EditText)view.findViewById(R.id.editText2);
String username =edit_username.getText().toString();
String password =edit_password.getText().toString();
Toast.makeText(MainActivity.this,username+"/n"+password, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder5.setNegativeButton("NO",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder5.show();
}


1 0
原创粉丝点击