new AlertDialog.Builder(MainActivity.this)//content
.setIcon(R.mipmap.ic_launcher)//Icon
.setTitle("确定")//Title
.setMessage("确定注册吗?")//Message
.setNeutralButton("返回", new DialogInterface.OnClickListener() {//返回键
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("退出", new DialogInterface.OnClickListener() {//取消键
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定键
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create().show();//创建并显示
setSingleChoiceItem(String,int,lisenter)方法实现,参数为选项,默认选中选项,事件监听器
String[] sex = new String[]{"男", "女"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("请选择性别");
builder.setCancelable(false);//必须进行选择
builder.setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//单选操作
}
});
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
setMultiChoiceItems(String[],boolean[],listenter)方法实现,参数分别为列表项,是否被选中,事件监听器
String[] addr = new String[]{"北京","上海","广州","深圳","杭州"};
new AlertDialog.Builder(MainActivity.this)
.setTitle("多选对话框")
.setMultiChoiceItems(addr, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
System.out.println(which);
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create().show();
setItem()指定一个Array对象即可实现列表项
new AlertDialog.Builder(MainActivity.this)
.setTitle("列表对话框")
.setItems(addr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="match_parent">
<SeekBar android:layout_width="350dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:id="@+id/seekBar" android:layout_gravity="center_horizontal" android:indeterminate="false" />
<TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="" android:id="@+id/textView" android:layout_gravity="center_horizontal" />
</LinearLayout>
Dialog dialog = new Dialog(this);
dialog.setTitle("拖动对话框");
dialog.setContentView(R.layout.seekbardialog);
SeekBar seekBar = (SeekBar) dialog.findViewById(R.id.seekBar);
seekBar.setMax(100);
final TextView textView = (TextView) dialog.findViewById(R.id.textView);
textView.setText("当前进度为:"+seekBar.getProgress());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText("当前进度为:" + seekBar.getProgress());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
dialog.show();
设置setAdapter()属性指定一个Adapter,Adapter可以是SimpleAdapter或者ArrayAdapter等Adapter
new AlertDialog.Builder(MainActivity.this)
.setTitle("简单对话框")
.setIcon(R.mipmap.ic_launcher)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setAdapter(new ArrayAdapter<String>(this,R.layout.cell,strings),null)
.create().show();
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TextView" android:textSize="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" />
使用setView()方法加入一个布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/head" android:layout_gravity="center_horizontal" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="You are my Sunshine" android:id="@+id/textView" android:layout_gravity="center_horizontal" />
</LinearLayout>
LinearLayout linerlayout = (LinearLayout) getLayoutInflater().inflate(R.layout.login, null);
new AlertDialog.Builder(MainActivity.this)
.setTitle("简单对话框")
.setIcon(R.mipmap.ic_launcher)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("hhhh");
}
})
.setView(linerlayout)
.create().show();