欢迎访问历史苑!微信公众号:yu118.com

android 8种对话框(Dialog)汇总

时间:2023-10-10 10:10:20编辑:yu118.com

<转>https://www.cnblogs.com/gzdaijie/p/5222191.html

1.代码示例image.png1.1普通Dialog普通对话框两个按钮
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 设置对话框图标             * @setTitle 设置对话框标题             * @setMessage 设置对话框消息提示             * setXXX方法返回Dialog对象,因此可以链式设置属性             */            final AlertDialog.Builder normalDialog =  new AlertDialog.Builder(MainActivity.this);            normalDialog.setIcon(R.drawable.icon_dialog);            normalDialog.setTitle("我是一个普通Dialog"  )            normalDialog.setMessage("你要点击哪一个按钮呢?"  );            normalDialog.setPositiveButton("确定",  new DialogInterface.OnClickListener() {                @Override                public void onClick(DialogInterface dialog, int which) {                    //...To-do                }            });            normalDialog.setNegativeButton("关闭",  new DialogInterface.OnClickListener() {                @Override                public void onClick(DialogInterface dialog, int which) {                    //...To-do                }            });            // 显示            normalDialog.show();        }  } 
三个按钮
//@setNeutralButton 设置中间的按钮 //若只需一个按钮,仅设置setPositiveButton即可 private void showMultiBtnDialog(){        AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainActivity.this);        normalDialog.setIcon(R.drawable.icon_dialog);        normalDialog.setTitle("我是一个普通Dialog").setMessage("你要点击哪一个按钮呢?");        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            }        });        // 创建实例并显示        normalDialog.show();(洋玩具是什么梗?该梗来自网上的一个脑筋急转弯,问:外国人叫洋人,外国酒叫洋酒,外国玩具叫什么?很多人会随口答出:洋玩具。)  }                         
本文专题:

下一篇:返回列表