Android--从零单排系列(4)--常用对话框和DialogFragment的优势
2016-05-31 17:26
531 查看
漂亮的对话框 sweet-alert-dialog
项目地址: https://github.com/pedant/sweet-alert-dialog
android原生的dialog太生硬了,之前看到了这个效果非常不错但是没有用过,今天给别人推荐使用,他遇到了问题,导入后错误非常多,也没有库工程。于是自己认真看了一下,这是个AndroidStudio的工程,并且里面还依赖于materialish-progress工程,也是个AS的工程。于是打算弄一个eclipse的版本并且将这两个工程融合在一起作为一个库工程XAlertDialogLibrary。使用时将其作为库导入项目中即可。(一):DialogFragment的使用
优点:
* 1,屏幕的选择和按下返回键能更好的管理它的什么周期方法 * 2,DialogFragment也允许开发者把Dialog作为内嵌的组件进行重用, 类似Fragment(可以在大屏幕和小屏幕显示出不同的效果) * 3,横竖屏切换,传统的new AlertDialog在屏幕旋转时, 第一不会保存用户输入的值, 而通过DialogFragment实现的对话框则可以完全不必考虑旋转的问题。
##使用方式
* 两种方式创建 * 1,onCreateView即使用定义的xml布局文件展示Dialog。 * 或者 * 2,onCreateDialog即利用AlertDialog或者Dialog创建出Dialog。
方式一:1,onCreateView即使用定义的xml布局文件展示Dialog。
第一步:
写一个EditNameDialogFragment(我没写的dialog) 集成DialogFragment
public class EditNameDialogFragment extends DialogFragment{ /** * 两种方式创建 * 1,onCreateView即使用定义的xml布局文件展示Dialog。 * 或者 * 2,onCreateDialog即利用AlertDialog或者Dialog创建出Dialog。 */ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //去掉线条 getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); View view = inflater.inflate(R.layout.dialog_fragment, container); return view; } }
第二步:写出DialogFragment的XML文件,然后在第一步引用即可完成
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/id_label_your_name" android:layout_width="wrap_content" android:layout_height="32dp" android:gravity="center_vertical" android:text="Your name:" /> <EditText android:id="@+id/id_txt_your_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/id_label_your_name" android:imeOptions="actionDone" android:inputType="text" /> <Button android:id="@+id/id_sure_edit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/id_txt_your_name" android:text="ok" /> </RelativeLayout>
方式二:2,onCreateDialog即利用AlertDialog或者Dialog创建出Dialog。
第一步:直接代码中解决,这里自定义了一个listenner实现Dialog数据传入到Activity中去
public class LoginLoginDialogFragment extends DialogFragment { private EditText mUsername; private EditText mPassword; public interface LoginInputListenner{ void onLoginInputListenner(String usename,String password); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); View view = getActivity().getLayoutInflater().inflate(R.layout.login_fragment, null); mUsername = (EditText) view.findViewById(R.id.id_txt_username); mPassword = (EditText) view.findViewById(R.id.id_txt_password); builder.setView(view).setPositiveButton("Sign in", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { LoginInputListenner listenner = (LoginInputListenner) getActivity(); listenner.onLoginInputListenner(mUsername .getText().toString(), mPassword .getText().toString()); } }).setNegativeButton("Cancel", null); return builder.create(); } }
第二步:第二种创建方式的XML文件布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="match_parent" android:layout_height="64dp" android:background="#FFFFBB33" android:contentDescription="@string/app_name" android:scaleType="center" /> <EditText android:id="@+id/id_txt_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="4dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginTop="16dp" android:hint="input username" android:inputType="textEmailAddress" /> <EditText android:id="@+id/id_txt_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginTop="4dp" android:fontFamily="sans-serif" android:hint="input password" android:inputType="textPassword" /> </LinearLayout>
*=========================================华丽的分割线===========================
(二):Dialog的使用
(1)简单警告框
public class MyDialogDemo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); // 调用布局管理器 Dialog dialog = new AlertDialog.Builder(this) .setTitle("对话框") // 创建标题 .setMessage("显示提示信息") // 表示对话框中的内容 .setIcon(R.drawable.pic_m) // 设置LOGO .create(); // 创建了一个对话框 dialog.show() ; // 显示对话框 } }
(2)多种操作按钮
public class MyDialogDemo extends Activity { private Button mybut = null ; // 定义按钮 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); // 调用布局管理器 this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮 this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类 } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this) .setTitle("确定删除?") // 创建标题 .setMessage("您确定要删除该条信息吗?") // 表示对话框中的内容 .setIcon(R.drawable.pic_m) // 设置LOGO .setPositiveButton("删除", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setNeutralButton("查看详情", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).create(); // 创建了一个对话框 dialog.show() ; // 显示对话框 } }
(3)退出按钮
public class MyDialogDemo extends Activity { private ImageButton but = null ; // 定义按钮 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); // 调用布局管理器 this.but = (ImageButton) super.findViewById(R.id.but) ; // 取得按钮 this.but.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类 } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { MyDialogDemo.this.exitDialog() ; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // 监听键盘 if (keyCode == KeyEvent.KEYCODE_BACK) { // 返回键 this.exitDialog() ; } return false ; } private void exitDialog(){ Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this) .setTitle("程序退出?") // 创建标题 .setMessage("您确定要退出本程序吗?") // 表示对话框中的内容 .setIcon(R.drawable.pic_m) // 设置LOGO .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MyDialogDemo.this.finish() ; // 操作结束 } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).create(); // 创建了一个对话框 dialog.show() ; // 显示对话框 } }
(4)列表对话框
public class MyDialogDemo extends Activity { private Button mybut = null ; // 定义按钮 private TextView mych = null ; // 定义文本 private String fruitData[] = new String[] { "苹果", "西瓜", "水蜜桃" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); // 调用布局管理器 this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮 this.mych = (TextView) super.findViewById(R.id.mych) ; // 取得文本 this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类 } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this) .setIcon(R.drawable.pic_m) .setTitle("请选择你喜欢吃的水果?") .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setItems(MyDialogDemo.this.fruitData, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MyDialogDemo.this.mych .setText("您选择的水果是:" + MyDialogDemo.this.fruitData[which]); } }).create() ; dialog.show() ; } } }
(5)单选对话框
public class MyDialogDemo extends Activity { private Button mybut = null ; // 定义按钮 private TextView mych = null ; // 定义文本 private TextView mytext = null ; // 定义文本 private String fruitData [] = new String[] { "苹果", "西瓜", "水蜜桃" }; private String fruitDesc [] = new String[] { "苹果,植物类水果,多次花果,具有丰富的营养成分,有食疗、辅助治疗等功能。", "西瓜(学名:Citrullus Lanatus,英文:Watermelon),属葫芦科,原产于非洲。", "水蜜桃,在植物分类学上属于蔷薇科,梅属,桃亚属,为落叶小乔木。"} ; private int chNum = 0 ; // 保存选项 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); // 调用布局管理器 this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮 this.mych = (TextView) super.findViewById(R.id.mych) ; // 取得文本 this.mytext = (TextView) super.findViewById(R.id.mytext) ; // 取得文本 this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类 } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this) .setIcon(R.drawable.pic_m) .setTitle("请选择你喜欢吃的水果?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MyDialogDemo.this.mych .setText(MyDialogDemo.this.fruitData[MyDialogDemo.this.chNum]); // 设置选项的名称 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setSingleChoiceItems(MyDialogDemo.this.fruitData, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MyDialogDemo.this.mytext .setText(MyDialogDemo.this.fruitDesc[which]); MyDialogDemo.this.chNum = which ; // 保存选项的索引 } }).create() ; dialog.show() ; } } }
(6)复选框对话框
public class MyDialogDemo extends Activity { private Button mybut = null ; // 定义按钮 private TextView mych = null ; // 定义文本 private String fruitData [] = new String[] { "苹果", "西瓜", "水蜜桃" }; private boolean chData[] = new boolean[] { true, true, false }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); // 调用布局管理器 this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮 this.mych = (TextView) super.findViewById(R.id.mych) ; // 取得文本 this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类 } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this) .setIcon(R.drawable.pic_m) .setTitle("请选择你喜欢吃的水果?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setMultiChoiceItems(MyDialogDemo.this.fruitData, MyDialogDemo.this.chData, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { for (int x = 0; x < MyDialogDemo.this.fruitData.length; x++) { if(x == which && isChecked) { // 当前选项被选中 MyDialogDemo.this.mych .append(MyDialogDemo.this.fruitData[x] + "、"); } } } }).create(); dialog.show() ; } } }
(7)进度对话框
public class MyDialogDemo extends Activity { private Button mybut = null ; // 定义按钮 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); // 调用布局管理器 this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮 this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类 } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { final ProgressDialog proDia = ProgressDialog.show(MyDialogDemo.this, "搜索网络", "请耐心等待..."); new Thread(){ public void run(){ // 线程的主体类 try { Thread.sleep(3000) ; // 运行三秒 } catch (Exception e) { } finally { proDia.dismiss() ; // 关闭对话框 } } }.start() ; proDia.show() ; // 显示对话框 } } }
(8)水平进度条
public class MyDialogDemo extends Activity { private Button mybut = null ; // 定义按钮 private static final int MAX_PROGRESS = 100 ; // 最大值 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); // 调用布局管理器 this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得按钮 this.mybut.setOnClickListener(new OnClickListenerImpl()) ; // 设置事件类 } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { final ProgressDialog proDia = new ProgressDialog(MyDialogDemo.this) ; proDia.setTitle("搜索网络") ; proDia.setMessage("请耐心等待") ; proDia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) ; // 水平进度条 proDia.setMax(MAX_PROGRESS) ; // 设置进度的最大值 proDia.setProgress(30) ; // 从进度30开始 proDia.setButton("后台处理", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { proDia.dismiss() ; // 关闭对话框 } }) ; proDia.setButton2("详细信息", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) ; proDia.onStart() ; // 启动进度 new Thread(){ public void run(){ // 线程的主体类 for (int x = 0; x < MAX_PROGRESS; x++) { try { Thread.sleep(500); // 运行三秒 } catch (Exception e) { } proDia.incrementProgressBy(1) ; } proDia.dismiss() ; } }.start() ; proDia.show() ; // 显示对话框 } }
相关文章推荐
- MooBox 基于Mootools的对话框插件
- 基于jQuery实现带动画效果超炫酷的弹出对话框(附源码下载)
- C#实现在前端网页弹出警告对话框(alert)的方法
- Android开发必知 九种对话框的实现方法
- Android中创建对话框(确定取消对话框、单选对话框、多选对话框)实例代码
- Android列表对话框用法实例分析
- C#使用Word中的内置对话框实例
- MFC对话框中添加状态栏的方法
- jquery实现点击弹出可放大居中及关闭的对话框(附demo源码下载)
- 基于BootStrap Metronic开发框架经验小结【六】对话框及提示框的处理和优化
- JavaScript弹出对话框的三种方式
- 用JavaScript实现对话框的教程
- Asp.net 弹出对话框基类(输出alet警告框)
- js弹出框、对话框、提示框、弹窗实现方法总结(推荐)
- boxy基于jquery的弹出层对话框插件扩展应用 弹出层选择器
- 自制轻量级仿jQuery.boxy对话框插件代码
- jQuery代码实现对话框右上角菜单带关闭×
- 基于jQuery的弹出警告对话框美化插件(警告,确认和提示)
- JS 模态对话框和非模态对话框操作技巧汇总
- jquery ui对话框实例代码