Android——自定义AlertDialog并与Activity进行数据通信
2016-12-23 09:53
567 查看
原文地址:http://blog.csdn.net/xuejingfu1/article/details/51597859
以下是官方文档的继承关系:
AlertDialog叫做“警告”对话框,常用于提示用户的某个操作会带来的后果,或者提示用户进行正确的操作。
正常情况下,大家经常看到的是这种情形:
其实现很简单,因为并没有改写任何东西,完全是系统提供的。示例代码如下:
[java] view
plain copy
print?
AlertDialog.Builder dialog = new AlertDialog.Builder (MainActivity.this);
dialog.setTitle("退出");
dialog.setIcon(R.drawable.exit_icon);
dialog.setMessage("是否退出当前应用?");
dialog.setCancelable(false);
dialog.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
}
});
dialog.setNegativeButton("Cancel", null);
dialog.show();
其中,setTitle设置对话框的标题,setIcon设置图标,setMessage设置对话框中的信息文本,setCancelable(false)指的是该对话框不会在点击“对话框以为区域”、“back键”后消失,而必须通过点击该对话框底部的按钮来收起对话框
系统默认支持在对话框底部添加3个按钮,其中常见的PositiveButton(位于最左侧)和NegativeButton(位于右侧),分别通过setPositiveButton和setNegativeButton来设置,它们的第一个参数是按钮上显示的文字,第二个参数通过实现DialogInterface.OnClickListener()方法来给该按钮添加点击事件。最后,对话框的Builder对象一定要调用show()方法,否则对话框无法显示。有时候我们希望从一个对话框来得到用户的输入,并进行相关的操作。比如,如下效果:
这时候就需要用到AlertDialog.Builder对象的一个很重要的方法setView(View v);其中参数v就是需要我们传入自定义的View对象。
一种简单便捷的方法是通过布局文件来自定义,然后用布局填充器把该布局加载进来并赋给一个View对象。
首先,在res/layout下创建alertdialog_layout.xml文件,添加自己想要在对话框中显示的控件:
[html] view
plain copy
print?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/alertDialog_et"
android:background="@drawable/bg_edittext"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="30dp"
android:textColor="#008A00"
android:maxLength="3"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入一个有效整数..."/>
</RelativeLayout>
如果你对以上EditText的属性含义不是很清晰,请点击:
http://blog.csdn.NET/xuejingfu1/article/details/51597032
这里仅仅添加了一个EditText,用于获取用户的输入,对话框底部的按钮当然也可以在该布局里添加。不过对于目前这个需求完全没有必要。
接下来就是引用这个自定义View,代码片段如下:
[java] view
plain copy
print?
final RelativeLayout customView=(RelativeLayout)getLayoutInflater()
.inflate(R.layout.alertdialog_layout,null);
new AlertDialog.Builder(this)
.setIcon(R.drawable.set_icon)
.setTitle("设置")
.setView(customView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText alarm_value = (EditText)customView.findViewById(R.id.alertDialog_et);
//这里进行输入判断
String str=alarm_value.getText().toString();
//这里添加对获取到的文本内容处理逻辑...
//你可以把此变量赋给一个静态变量,来实现与Activity的通信
})
.setNegativeButton("Cancel", null)
.setCancelable(false)
.create()
.show();
注意:这里通过id来查找控件时,是在customView里面找。以上红色标记处!
以下是官方文档的继承关系:
AlertDialog叫做“警告”对话框,常用于提示用户的某个操作会带来的后果,或者提示用户进行正确的操作。
正常情况下,大家经常看到的是这种情形:
其实现很简单,因为并没有改写任何东西,完全是系统提供的。示例代码如下:
[java] view
plain copy
print?
AlertDialog.Builder dialog = new AlertDialog.Builder (MainActivity.this);
dialog.setTitle("退出");
dialog.setIcon(R.drawable.exit_icon);
dialog.setMessage("是否退出当前应用?");
dialog.setCancelable(false);
dialog.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
}
});
dialog.setNegativeButton("Cancel", null);
dialog.show();
其中,setTitle设置对话框的标题,setIcon设置图标,setMessage设置对话框中的信息文本,setCancelable(false)指的是该对话框不会在点击“对话框以为区域”、“back键”后消失,而必须通过点击该对话框底部的按钮来收起对话框
系统默认支持在对话框底部添加3个按钮,其中常见的PositiveButton(位于最左侧)和NegativeButton(位于右侧),分别通过setPositiveButton和setNegativeButton来设置,它们的第一个参数是按钮上显示的文字,第二个参数通过实现DialogInterface.OnClickListener()方法来给该按钮添加点击事件。最后,对话框的Builder对象一定要调用show()方法,否则对话框无法显示。有时候我们希望从一个对话框来得到用户的输入,并进行相关的操作。比如,如下效果:
这时候就需要用到AlertDialog.Builder对象的一个很重要的方法setView(View v);其中参数v就是需要我们传入自定义的View对象。
一种简单便捷的方法是通过布局文件来自定义,然后用布局填充器把该布局加载进来并赋给一个View对象。
首先,在res/layout下创建alertdialog_layout.xml文件,添加自己想要在对话框中显示的控件:
[html] view
plain copy
print?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/alertDialog_et"
android:background="@drawable/bg_edittext"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="30dp"
android:textColor="#008A00"
android:maxLength="3"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入一个有效整数..."/>
</RelativeLayout>
如果你对以上EditText的属性含义不是很清晰,请点击:
http://blog.csdn.NET/xuejingfu1/article/details/51597032
这里仅仅添加了一个EditText,用于获取用户的输入,对话框底部的按钮当然也可以在该布局里添加。不过对于目前这个需求完全没有必要。
接下来就是引用这个自定义View,代码片段如下:
[java] view
plain copy
print?
final RelativeLayout customView=(RelativeLayout)getLayoutInflater()
.inflate(R.layout.alertdialog_layout,null);
new AlertDialog.Builder(this)
.setIcon(R.drawable.set_icon)
.setTitle("设置")
.setView(customView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText alarm_value = (EditText)customView.findViewById(R.id.alertDialog_et);
//这里进行输入判断
String str=alarm_value.getText().toString();
//这里添加对获取到的文本内容处理逻辑...
//你可以把此变量赋给一个静态变量,来实现与Activity的通信
})
.setNegativeButton("Cancel", null)
.setCancelable(false)
.create()
.show();
注意:这里通过id来查找控件时,是在customView里面找。以上红色标记处!
相关文章推荐
- Android——自定义AlertDialog并与Activity进行数据通信
- android 自定义AlertDialog 与Activity相互传递数据
- Android Intent 程序之间进行通信 传输数据
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- 与android客户端以JSON格式进行数据通信
- 67 Android service 和 activity 的数据通信
- Android:在不同的Activity之间进行数据的共享
- Android 两个Activity进行数据传送 发送
- Android入门:通过XML数据与服务器进行通信
- Android网络开发中如何使用JSON进行网络通信---Android_JSON数据通讯方法解析
- Android网络开发中如何使用JSON进行网络通信---Android JSON数据通讯方法解析
- Android 的Application之一 ---- 在不同的类(Activity界面)中进行数据的操作
- Android中在两个Activity之间进行数据传递
- Android网络开发中如何使用JSON进行网络通信---Android JSON数据通讯方法解析
- AndroidのActivity跳转数据通信和跳转动画
- [android]Android怎样能有两个Activity传递的自定义数据
- 【android】Activity之间的通信与自定义Action
- Android中aidl实现Activity与service进行通信和回调
- android的Activity之间的通信与自定义Action
- AndroidのActivity跳转数据通信和跳转动画