此例子源于网络,下载下来之后,自己加了写注释,作为总结,发到博客中,谢谢原作者

通过这个例子学到的东西

1.自定义对话框的使用

2.程序中使用颜色如何进行存放,增加复用性

3.加深线性布局、常用控件的使用

1.实现效果

2.颜色值文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="white">#FFFFFF</drawable>
<color name="White">#FFFFFF</color>
<color name="Black">#000000</color>
<color name="grey">#D7D4D4</color>
<color name="red">#FF0000</color> </resources>

3.第一个界面布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:orientation="vertical"> <LinearLayout
android:orientation="vertical"
android:background="@drawable/alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"> <TextView android:id="@+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="17sp"
android:text="About to call 323"/> <TextView android:id="@+id/dialog_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:gravity="center_horizontal"
android:textSize="15sp"
android:textColor="#ffffff"
android:text="Are you sure you want to proceed?" /> <Button
android:id="@+id/ok"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="@drawable/custom_button"
android:textColor="@color/White"
android:textSize="17sp"
android:textStyle="bold"
android:text="OK"/>
</LinearLayout>
</LinearLayout>

4.第二个界面的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:orientation="vertical"> <LinearLayout
android:orientation="vertical"
android:background="@drawable/alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"> <TextView android:id="@+id/dialog_title_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="17sp"
android:text="About to call 323"/> <TextView android:id="@+id/dialog_message_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:gravity="center_horizontal"
android:textSize="15sp"
android:textColor="#ffffff"
android:text="Are you sure you want to proceed?" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:orientation="horizontal" > <Button
android:id="@+id/cancel_2"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_gravity="left"
android:layout_marginLeft="10dip"
android:layout_weight="1"
android:background="@drawable/custom_button"
android:text="取消"
android:textColor="@color/White"
android:textStyle="bold" /> <Button
android:id="@+id/ok_2"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_marginBottom="10dip"
android:layout_marginRight="10dip"
android:layout_weight="1"
android:background="@drawable/custom_button"
android:text="确定"
android:textColor="@color/White"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

5.核心代码文件

public class MainActivity extends Activity {

    private Button btn;
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn1); /**增加监听事件**/
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View view) {
// TODO Auto-generated method stub
showCustomMessageOK("提示信息","不能进行此项操作");
} }); btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
showCustomMessageOKAndCancle("温馨提示","是否确认退出");
}
});
} /**
* 实现一个带有确定和取消按钮的对话框
* @param title
* @param message
*/
protected void showCustomMessageOKAndCancle(String title, String message) {
// TODO Auto-generated method stub /**
* 创建一个Dialog对象,Dialog有两个构造法方法
* 1.
*
*
**/
final Dialog dialog = new Dialog(MainActivity.this, android.R.style.Theme_Translucent_NoTitleBar); /**为Dialog加载布局文件**/
dialog.setContentView(R.layout.ok_cancle_dialog_view);
/**为设置相应的属性值**/
((TextView)dialog.findViewById(R.id.dialog_title_2)).setText(title);
((TextView)dialog.findViewById(R.id.dialog_message_2)).setText(message);
((Button) dialog.findViewById(R.id.cancel_2)).setOnClickListener(new OnClickListener() { @Override
public void onClick(View view) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}); ((Button) dialog.findViewById(R.id.ok_2)).setOnClickListener(new OnClickListener() { @Override
public void onClick(View view) {
// TODO Auto-generated method stub
MainActivity.this.finish();
System.exit(0);
}
});
dialog.show();
}
/**
* 创建一个只有确定按钮的对话框
* @param title
* @param message
*/
private void showCustomMessageOK(String title, String message) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(MainActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.ok_dialog_view);
((TextView) dialog.findViewById(R.id.dialog_title)).setText(title);
((TextView) dialog.findViewById(R.id.dialog_message)).setText(message);
((Button) dialog.findViewById(R.id.ok)).setText("OK");
((Button) dialog.findViewById(R.id.ok)).setOnClickListener(new OnClickListener() { @Override
public void onClick(View view) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}); dialog.show();
}
}

代码下载地址

Android简单例子——IpHone样式AlertDialog的更多相关文章

  1. Android简单例子——AlertDialog

    最近学习了一段时间的Android,在网上找了些小的Demo,自己模拟这做了下,首先谢谢那些提供例子的朋友 今天主要学习的是简单的Dialog的使用(实现退出对话框)和自定义对话框 1.实现退出对话框 ...

  2. android 简单列表对话框(AlertDialog.Builder().setItems())

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  3. android json解析及简单例子+Android与服务器端数据交互+Android精彩案例【申明:来源于网络】

    android json解析及简单例子+Android与服务器端数据交互+Android精彩案例[申明:来源于网络] android json解析及简单例子:http://www.open-open. ...

  4. Android 自定义RadioButton的样式

    Android 自定义RadioButton的样式 我们知道Android控件里的button,listview可以用xml的样式自定义成自己希望的漂亮样式. 最近用到RadioButton,利用xm ...

  5. Android中实现全屏、无标题栏的两种办法(另附Android系统自带样式的解释)

    在进行UI设计时,我们经常需要将屏幕设置成无标题栏或者全屏.要实现起来也非常简单,主要有两种方法:配置xml文件和编写代码设置. 1.在xml文件中进行配置 在项目的清单文件AndroidManife ...

  6. Android简单逐帧动画Frame的实现(二)

    Android简单逐帧动画Frame的实现   Android简单逐帧动画Frame的实现 1.逐帧动画 即是通过播放预先排序好的图片来实现动态的画面,感觉像是放电影. 2.实现步骤: 1. 在工程里 ...

  7. Android中无标题样式和全屏样式学习

    在进行UI设计时,我们经常需要将屏幕设置成无标题栏或者全屏.要实现起来也非常简单,主要有两种方法:配置xml文件和编写代码设置. 1.在xml文件中进行配置 在项目的清单文件AndroidManife ...

  8. Hibernate4.2.4入门(一)——环境搭建和简单例子

    一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...

  9. AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答

    一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...

随机推荐

  1. 在Eclipse中安装和使用TFS插件

    在Eclipse中安装插件的方法其实都一样,安装TFS的步骤如下: 下载TFS插件.你可以到微软的下载中心,下载TFS插件TFSEclipsePlugin-UpdateSiteArchive-10.0 ...

  2. Ignatius and the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  3. ASP.NET内部原理(HttpHandler和HttpModule)

    [IT168 技术文档]在以前的ASP时候,当请求一个*.asp页面文件的时候,这个HTTP请求首先会被一个名为 inetinfo.exe进程所截获,这个进程实际上就是www服务.截获之后它会将这个请 ...

  4. JavaScript高级程序设计50.pdf

    hashchange事件 HTML5新增了hashchange事件,以便在URL的参数列表(及URL中“#”号后面的所有字符串)发生变化时通知开发人员,之所以新增这个事件,是因为在Ajax应用中,开发 ...

  5. [置顶] cocos2dx sqllite 增删查改等操作

    首先导入文件shell.c sqllite3.c sqlite3.h sqlite3etx.h文件(注意在生成安卓项目是 不要将shell.c写进android.mk文件中,写进去在cywin中生成会 ...

  6. opencv学习笔记-图像对比度、亮度调节

    在数学中我们学过线性理论,在图像亮度和对比度调节中同样适用,看下面这个公式: 在图像像素中其中: 参数f(x)表示源图像像素. 参数g(x) 表示输出图像像素. 参数a(需要满足a>0)被称为增 ...

  7. ACMDP之最长公共子序列长度—HDU1159

    Common Subsequence Problem Description A subsequence of a given sequence is the given sequence with ...

  8. sql优化-hint的作用

    目前,oracle采用的是CBR优化器,所以在有些时候,机器会按照自己的意愿去执行sql,当然oracle是根据本身的一些信息来做决定的,比如:统计信息.但有些时候,机器并不一定会按照我们预想的那样去 ...

  9. android开发:@SuppressLint( NewApi )

    这个是android带的lint工具提示的,lint官方的说法是 Improving Your Code with lint,应该是帮助提升代码的 ,如果不想用的话,可以右键点工程,然后在androi ...

  10. CSS 边框 阴影 效果

    CSS 边框 阴影 效果 以下将css实现阴影效果,以便须要朋友们,直接上代码 #shadow1{ width: 200px; height: 100px; color: white; backgro ...