安卓常用弹出窗

1.提示
先来说一下“提示”(提示几秒后就会消失),也许你只是想提示一下,不需要对方反馈,甚至不需要对方一定看见,也许你需要的是这个:

[java] view plain copy print ?
  1. Toast.makeText(QueryCarInfoActivity.this, "上传数据成功", Toast.LENGTH_SHORT).show();  
  2. //从资源文件string.xml 里面取提示信息 
  3. Toast.makeText(QueryCarInfoActivity.this, getString(R.string.hello), Toast.LENGTH_SHORT).show(); 

参数:
Toast.LENGTH_SHORT: Show the view or text notification for a short period of time
Toast.LENGTH_LONG: Show the view or text notification for a long period of time

2.使用new AlertDialog.Builder(context)创建一个对话框
(1)确定框
点击时,弹出一个消息框

[java] view plain copy print ?
  1. /**
  2. * 确定框
  3. */ 
  4. private void dialogShowOK() { 
  5.     new AlertDialog.Builder(this
  6.     .setTitle("确定框"
  7.     .setMessage("简单消息框"
  8.     .setPositiveButton("确定", new DialogInterface.OnClickListener() { 
  9.          
  10.         public void onClick(DialogInterface dialog, int which) { 
  11.             //按钮事件 
  12.         } 
  13.     }) 
  14.     .show(); 

(2)确定取消框
确定还是取消,it is a problem,当你做某项操作时,弹出一个提示,来确保操作无误。如删除文件时,提示“确认删除文件”;当按返回按钮时,提示“确认退出”等。

[java] view plain copy print ?
  1.     /**
  2.      * 确定取消框
  3.      */ 
  4.     private void dialogShowOKCancel() { 
  5.         new AlertDialog.Builder(this
  6.         .setTitle("确定取消框"
  7.         .setIcon(R.drawable.down) 
  8.         .setMessage("确定退出吗"
  9.         .setPositiveButton("确定", new DialogInterface.OnClickListener() { 
  10.              
  11.             public void onClick(DialogInterface dialog, int which) { 
  12.                 //确定按钮事件 
  13.                 setResult(RESULT_OK); 
  14. //              dialog.dismiss(); 
  15.                 finish(); 
  16.             } 
  17.         }) 
  18.         .setNegativeButton("取消", new DialogInterface.OnClickListener() { 
  19.              
  20.             public void onClick(DialogInterface dialog, int which) { 
  21.                 //取消按钮事件 
  22. //              dialog.dismiss(); 
  23.             } 
  24.         }) 
  25.         .show(); 
  26.     } 
  27.  
  28.     /**
  29.      * 按返回键提示是否退出
  30.      */ 
  31.     @Override 
  32.     public boolean onKeyDown(int keyCode, KeyEvent event) { 
  33.         if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
  34.                dialogShowOKCancel(); 
  35.         } 
  36.         return false
  37.     } 

(3)多按钮
改变了对话框的图表,添加了三个按钮

[java] view plain copy print ?
  1. /**
  2. * 多按钮
  3. */ 
  4. private void dialogShowButton() { 
  5.     new AlertDialog.Builder(this
  6.     .setIcon(R.drawable.down) 
  7.     .setTitle("文件"
  8.     .setMessage("请选择您的操作:"
  9.     .setPositiveButton("保存", new DialogInterface.OnClickListener() { 
  10.          
  11.         public void onClick(DialogInterface dialog, int which) { 
  12.             // TODO Auto-generated method stub 
  13.             Toast.makeText(TestDialogActivity.this, "保存文件", Toast.LENGTH_LONG).show(); 
  14.         } 
  15.     }) 
  16.     .setNegativeButton("返回", new DialogInterface.OnClickListener() { 
  17.          
  18.         public void onClick(DialogInterface dialog, int which) { 
  19.             // TODO Auto-generated method stub 
  20.             Toast.makeText(TestDialogActivity.this, "返回继续编辑文件", Toast.LENGTH_LONG).show(); 
  21.         } 
  22.     }) 
  23.     .setNeutralButton("清空", new DialogInterface.OnClickListener() { 
  24.          
  25.         public void onClick(DialogInterface dialog, int which) { 
  26.             // TODO Auto-generated method stub 
  27.             Toast.makeText(TestDialogActivity.this, "清空文件", Toast.LENGTH_LONG).show(); 
  28.         } 
  29.     }) 
  30.     .show();     

安卓常用弹出窗_第1张图片

(4)输入框
通过setView()方法,为对话框传入了一个文本编辑框,当然,你可以传入任意视图对象,如图片框。

[java] view plain copy print ?
  1. /**
  2. * 输入框
  3. */ 
  4. private void dialogShowInput() { 
  5.     new AlertDialog.Builder(this
  6.     .setTitle("请输入"
  7.     .setIcon(R.drawable.info) 
  8.     .setView(new EditText(this)) 
  9.     .setPositiveButton("确定", null
  10.     .setNegativeButton("取消", null
  11.     .show(); 


上面的两个null参数,这里要放的其实是这两个按钮点击的监听程序,由于我们这里不需要监听这些动作,所以传入null值简单忽略掉,但是实际开发的时候一般都是需要传入监听器的,用来响应用户的操作。

安卓常用弹出窗_第2张图片

(5)图片框

[java] view plain copy print ?
  1. /**
  2. * 图片框
  3. */ 
  4. private void dialogShowImage() { 
  5.     ImageView imgView = new ImageView(this); 
  6.     imgView.setImageResource(R.drawable.image); 
  7.     new AlertDialog.Builder(this
  8.     .setTitle("图片框"
  9.     .setView(imgView) 
  10.     .setPositiveButton("确定", null
  11.     .show(); 

安卓常用弹出窗_第3张图片

(6)单选框

[java] view plain copy print ?
  1. /**
  2. * 单选框
  3. */ 
  4. private void dialogShowRadio() { 
  5.     String[] s = new String[] {"选项1", "选项2", "选项3", "选项4"}; 
  6.     new AlertDialog.Builder(this
  7.     .setTitle("单选框"
  8.     .setIcon(R.drawable.down) 
  9.     .setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() { 
  10.          
  11.         public void onClick(DialogInterface dialog, int which) { 
  12.             // TODO Auto-generated method stub 
  13.             dialog.dismiss(); 
  14.         } 
  15.     }) 
  16.     .setNegativeButton("取消", null
  17.     .show(); 

(7)多选框

[java] view plain copy print ?
  1. /**
  2. * 多选框
  3. */ 
  4. private void dialogShowCheck() { 
  5.     String[] s = new String[] {"选项1", "选项2", "选项3", "选项4"}; 
  6.     new AlertDialog.Builder(this
  7.     .setTitle("多选框"
  8.     .setIcon(R.drawable.down) 
  9.     .setMultiChoiceItems(s, null, null
  10.     .setPositiveButton("确定", null
  11.     .setNegativeButton("取消", null
  12.     .show(); 

安卓常用弹出窗_第4张图片

(8)列表框

[java] view plain copy print ?
  1. /**
  2. * 列表框
  3. */ 
  4. private void dialogShowList() { 
  5.     String[] s = new String[] {"列表1", "列表2", "列表3"}; 
  6.     new AlertDialog.Builder(this
  7.     .setTitle("列表框"
  8.     .setIcon(R.drawable.down) 
  9.     .setItems(s, null
  10.     .setPositiveButton("确定", null
  11.     .show(); 

安卓常用弹出窗_第5张图片

你可能感兴趣的:(安卓常用弹出窗)