
九種對(duì)話框.png
基本的 alertdialog 使用
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonNormal = (Button) findViewById(R.id.button_normal);
buttonNormal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNormalDialog();
}
});
}
private void showNormalDialog(){
/* @setIcon 設(shè)置對(duì)話框圖標(biāo)
* @setTitle 設(shè)置對(duì)話框標(biāo)題
* @setMessage 設(shè)置對(duì)話框消息提示
* setXXX方法返回Dialog對(duì)象,因此可以鏈?zhǔn)皆O(shè)置屬性
*/
final AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("我是一個(gè)普通Dialog")
normalDialog.setMessage("你要點(diǎn)擊哪一個(gè)按鈕呢?");
normalDialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
normalDialog.setNegativeButton("關(guān)閉",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
// 顯示
normalDialog.show();
}
}
3 個(gè)按鈕
/* @setNeutralButton 設(shè)置中間的按鈕
* 若只需一個(gè)按鈕,僅設(shè)置 setPositiveButton 即可
*/
private void showMultiBtnDialog(){
AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("我是一個(gè)普通Dialog").setMessage("你要點(diǎn)擊哪一個(gè)按鈕呢?");
normalDialog.setPositiveButton("按鈕1",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
normalDialog.setNeutralButton("按鈕2",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
normalDialog.setNegativeButton("按鈕3", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
// 創(chuàng)建實(shí)例并顯示
normalDialog.show();
}
參考文章
android 8 種對(duì)話框(Dialog)使用方法匯總 - 呆尐兔兔 - 博客園
https://www.cnblogs.com/gzdaijie/p/5222191.html