AlertDialog的几种形式
1.基本的样式
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAlertDialog1"
android:text="3个按钮的AlertDialog" />
public void showAlertDialog1(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() { //正面的按钮,如 确定,是 之类的
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Positive", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Negative", new DialogInterface.OnClickListener() { //反面的按钮,如 取消,否 之类的
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Negative", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() { //中立的按钮,如 不确定 之类的
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Neutral", Toast.LENGTH_SHORT).show();
}
});
// AlertDialog alertDialog = builder.create(); //这两句与下面的一句相同
// alertDialog.show();
builder.show();
}
2.列表的样式
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAlertDialog2"
android:text="列表的AlertDialog" />
public void showAlertDialog2(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
// builder.setMessage("Message"); 这句不能加,加上就不会显示列表了
final String s[] = {"Android", "IOS", "Windows Phone"};
builder.setItems(s, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
3.选项列表的样式
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAlertDialog3"
android:text="选项列表的AlertDialog" />
public void showAlertDialog3(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String s[] = {"Android", "IOS", "Windows Phone"};
final ArrayList<String> list = new ArrayList<>();
builder.setMultiChoiceItems(s, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked)
list.add(s[which]);
else
list.remove(s[which]);
}
});
builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, list.toString(), Toast.LENGTH_SHORT).show();
list.clear();
}
});
builder.show();
}
4.单选列表的样式
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAlertDialog4"
android:text="单选列表的AlertDialog" />
public void showAlertDialog4(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
final String s[] = {"Android", "IOS", "Windows Phone"};
builder.setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() { //0代表初始是选择第一个
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
5.自定义的样式
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAlertDialog5"
android:text="自定义的AlertDialog" />
在drawable目录下新建alert_dialog_layout 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:orientation="vertical">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/ic_launcher"
android:hint="Username"
android:inputType="text" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/ic_launcher"
android:hint="Password"
android:inputType="textPassword" />
</LinearLayout>
public void showAlertDialog5(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View view = getLayoutInflater().inflate(R.layout.alert_dialog_layout, null); //注意,需定义为final
builder.setView(view);
// builder.setView(R.layout.alert_dialog_layout); 在5.0以上使用
builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText username = (EditText) view.findViewById(R.id.username); //在view中findId
EditText password = (EditText) view.findViewById(R.id.password);
String string = username.getText().toString() +"---"+ password.getText().toString();
Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
AlertDialog的几种使用形式
最新推荐文章于 2022-12-22 09:46:19 发布