您的位置:首页 > 移动开发 > Android开发

Android对话框

2017-10-24 09:53 127 查看

对话框

2017/9/29 13:43:02

确定取消

效果



代码

//创建对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//设置图标
builder.setIcon(R.mipmap.ic_launcher);
//设置标题
builder.setTitle("确定退出!");
//设置确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_LONG).show();
}
});

//设置取消按钮 如果只是要取消对话框 点击事件可以传NULL
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_LONG).show();
}
});
//显示对话框
builder.show();


多按钮

效果



代码

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setIcon(R.mipmap.ic_launcher);

builder.setTitle("提示");

builder.setMessage("是否保存文件?");

builder.setPositiveButton("是", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialogInterface, int i) {

Toast.makeText(MainActivity.this, "是", Toast.LENGTH_LONG).show();

}

});
builder.setNegativeButton("否", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialogInterface, int i) {

Toast.makeText(MainActivity.this, "否", Toast.LENGTH_LONG).show();

}
});
builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialogInterface, int i) {

Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_LONG).show();
}
});

builder.show();


列表框

效果



代码

//条目数组
final String[] arr = new String[]{
"香蕉", "苹果", "橘子", "西瓜", "葡萄"
};

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setTitle("请选择你喜欢的水果!");
//设置条目
builder.setItems(arr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, arr[i], Toast.LENGTH_LONG).show();
}
});
//创建对话框并且显示
builder.create().show();


单选列表框

效果



代码

//条目数据
final String[] arr = new String[]{
"香蕉", "苹果", "橘子", "西瓜", "葡萄"
};

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setTitle("请选择你喜欢的水果!");
//设置条目
builder.setSingleChoiceItems(arr, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, arr[i], Toast.LENGTH_LONG).show();

}
});

builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});

builder.setNeutralButton("取消", null);

builder.create().show();


进度条

效果



代码

final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

//设置标题
progressDialog.setTitle("正在下载...");

//设置风格
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

//设置最大大进度
progressDialog.setMax(100);

progressDialog.setButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});

progressDialog.setButton2("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});

progressDialog.show();

//创建线程模拟下载进度
new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while (i <= 100) {

i++;
//设置进度
progressDialog.incrementProgressBy(i);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

}
}).start();


多选列表框

效果



代码

//条目数据
final String[] arr = new String[]{
"香蕉", "苹果", "橘子", "西瓜", "葡萄"
};

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setTitle("请选择你喜欢的水果!");
//设置条目
builder.setMultiChoiceItems(arr, new boolean[]{false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {

if (b) {

Toast.makeText(MainActivity.this, arr[i], Toast.LENGTH_LONG).show();

}

}
});

builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});

builder.setNeutralButton("取消", null);

builder.create().show();


自定义消息框

效果



代码

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View view1 = View.inflate(MainActivity.this, R.layout.item, null);
builder.setView(view1);

EditText et_user = view1.findViewById(R.id.et_user);
EditText et_pass = view1.findViewById(R.id.et_pass);

Button btn_login = view1.findViewById(R.id.btn_login);
Button btn_right = view1.findViewById(R.id.btn_right);

final AlertDialog alertDialog = builder.create();
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
alertDialog.dismiss();
}
});

btn_right.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_LONG).show();
alertDialog.dismiss();
}
});
alertDialog.show();


XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical"
android:padding="10dp">

<EditText
android:hint="请输入用户名"
android:id="@+id/et_user"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:hint="请输入密码"
android:id="@+id/et_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<LinearLayout

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"

>

<Button
android:id="@+id/btn_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登陆" />

<Button
android:id="@+id/btn_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="注册" />

</LinearLayout>

</LinearLayout>


读取进度框

效果



代码

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

progressDialog.setTitle("正在读取");

progressDialog.setIndeterminate(true);

progressDialog.setCancelable(true);

progressDialog.show();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: