哪些年遇到过的Andriod问题(4)对话框 dialog
来源:互联网 发布:北京指南针软件怎么样 编辑:程序博客网 时间:2024/05/16 00:34
对话框一共有三个按键:
setPositiveButton确认;setNegativeButton取消;setNeutralButton一般
中间的询问内容可以是setMessage也可以是控件(setView/ setMultiChoiceItems/ setItems),还可以是自定义的一个布局。
测试过可以直接使用,但是在Eclipse中提示代码有问题,那就是你的引用问题,如:DialogInterface.OnClickListener()
//创建对话框方法dialog() protected void dialog() { AlertDialog.Builder builder = new Builder(Main.this); builder.setMessage("确认退出吗?"); builder.setTitle("提示"); builder.setPositiveButton("确认", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Main.this.finish(); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } //在onKeyDown(int keyCode, KeyEvent event)方法中调用此方法 public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { dialog(); } return false;}
//改变了对话框的图表,添加了三个按钮Dialog dialog = new AlertDialog.Builder(this).setIcon( android.R.drawable.btn_star).setTitle("喜好调查").setMessage( "你喜欢李连杰的电影吗?").setPositiveButton("很喜欢", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(Main.this, "我很喜欢他的电影。", Toast.LENGTH_LONG).show(); } }).setNegativeButton("不喜欢", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(Main.this, "我不喜欢他的电影。", Toast.LENGTH_LONG) .show(); } }).setNeutralButton("一般", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(Main.this, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG) .show(); } }).create();dialog.show();
//信息内容是一个简单的View类型new AlertDialog.Builder(this).setTitle("请输入").setIcon( android.R.drawable.ic_dialog_info).setView( new EditText(this)).setPositiveButton("确定", null).setNegativeButton("取消", null).show();
new AlertDialog.Builder(this).setTitle("复选框").setMultiChoiceItems( new String[] { "Item1", "Item2" }, null, null) .setPositiveButton("确定", null).setNegativeButton("取消", null).show();
//信息内容是一组多选框new AlertDialog.Builder(this).setTitle("单选框").setIcon( android.R.drawable.ic_dialog_info).setSingleChoiceItems( new String[] { "Item1", "Item2" }, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).setNegativeButton("取消", null).show();
new AlertDialog.Builder(this).setTitle("列表框").setItems( new String[] { "Item1", "Item2" }, null).setNegativeButton("确定", null).show();
//信息内容是一个自定义的布局//1.布局文件<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="#ffffffff" android:orientation="horizontal" android:id="@+id/dialog"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tvname" android:text="姓名:" /> <EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/></LinearLayout>//2.调用代码LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.dialog, (ViewGroup) findViewById(R.id.dialog)); new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout) .setPositiveButton("确定", null).setNegativeButton("取消", null).show();
图8 以上几种对话框有点难了吧(初学者),这是种简单的提示框(为什么简单?因为代码和打开一般的界面一样)
//1. 为什么简单?因为代码和打开一般的界面一样bOpenCD = (Button) findViewById(R.id.maOpenCD);bOpenCD.setOnClickListener(new OnClickListener() {public void onClick(View v) {Intent myIntent = new Intent();myIntent.setClass(MainActivity.this, CustomDialog.class);MainActivity.this.startActivity(myIntent);}});//2.修改manifest文件 <activity android:name=".CustomDialog" android:label="@string/CDlable" android:theme = "@android:style/Theme.Dialog"><!-- @style/Transparent --> </activity>
图9透明提示窗体
摘于http://blog.csdn.net/sodino/article/details/5822147
//1.先在res/values下建colors.xml文件,写入<resources> <color name="transparent">#9000</color></resources>//这个值设定了整个界面的透明度,为了看得见效果,现在设为透明度为56%(9/16)左右。//2.再在res/values/下建styles.xml,设置程序的风格<resources> <style name="Transparent"> <item name="android:windowBackground">@color/transparent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item> </style></resources>//3.最后一步,把这个styles.xml用在相应的Activity上。即在AndroidManifest.xml中的任 意<activity>标签中添加<activity android:name=".CustomDialog" android:label="@string/CDlable" android:theme = "@style/Transparent"><!-- @android:style/Theme.Dialog--> </activity>
- 哪些年遇到过的Andriod问题(4)对话框 dialog
- 哪些年遇到过的Andriod问题(12)布局
- 哪些年遇到过的Andriod问题(一)页面跳转与数据传输
- 哪些年遇到过的Andriod问题(三)Please ensure that adb is correctly located
- 哪些年遇到过的Andriod问题(6)Android特效 五种Toast详解
- 哪些年遇到过的Andriod问题(7)ProgressBar 史上最强大讲解
- 哪些年遇到过的Andriod问题(9)R.java文件不能自动生成解决方法
- 哪些年遇到过的Andriod问题(16)Eclipse如何调试
- 哪些年遇到过的Andriod问题(二)在新开页面中输入的值返回到原页面
- 哪些年遇到过的Andriod问题(5)This text field does not specify an inputType or a hint
- 哪些年遇到过的Andriod问题(8)Handler使用 mars.barhandler中的错误(测试) 线程中停止线程?
- 哪些年遇到过的Andriod 名词(14) DDMS Dalvik ContentProvide SAX
- 哪些年遇到过的Andriod问题(13)Mars的下载文件代码不知道是什么问题,在网上去新COPY了一个就可以下载了。
- 哪些年遇到过的Andriod问题(15)注册广播服务,如果你看过MARS老师的视频,还是不算了吧,这是纯COPY的
- 哪些年遇到过的Andriod问题(十)如何让自定义的方法/类在外部 (别的命名空间) 调用时,鼠标移上去会有提示?
- 哪些年遇到过的Andriod问题(11)如何下载文件到SD卡中?权限集?问题在下载网络中1K的文件时要假死?(Mars的代码有问题吗)
- andriod对话框,弹出框dialog以及点击item的响应
- 遇到过的问题
- release版本与debug版本的区别
- c#(asp.net)杂谈笔记
- 大量数据导出EXCEL,原始JDBC,导出CSV
- 动态获取字符串 NSString的长宽--------自适应文本宽度、高度
- Hadoop0.23.0初探2---HDFS Federation部署
- 哪些年遇到过的Andriod问题(4)对话框 dialog
- hdu 4362
- liunx学习第三天笔记
- android adb shell 命令大全
- 一个伪内存泄漏问题
- 各种骗术及防骗手册
- 湄公河发现怪异鱼类生殖器官长在头部
- Oracle三层嵌套分页的理解
- 【开源】博客导出工具