Android UI 之 Dialog
来源:互联网 发布:淘宝好不好 编辑:程序博客网 时间:2024/05/22 08:17
Dialog控件就是一个对话框,显示在屏幕中间的小窗体,其宽高由其内容决定,所有的对话框类都直接或间接继承Dialog类。
一、AlertDialog
AlertDialog直接继承于Dialog类,通过使用Builder类来构建AlertDialog
下面是一个简单的AlertDialog的实现:
public void titleDialog(){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("Info Title"); builder.setMessage(R.string.info); builder.create().show();}
先创建builder对象,然后使用setTitle方法设置对话框的标题,setMessage方法设置对话框的内容,最后通过create方法构建AlertDialog对象,如果要显示出对话框就调用show方法。
AlertDialog除了上面的方法还有以下这些常用的方法:
设置按钮的方法:
setPositiveButton():一个积极的按钮,一般用于“OK”或者“继续”等操作。
setNegativeButton():一个负面的按钮,一般用于“取消”操作。
setNeutralButton():一个比较中性的按钮,一般用于“忽略”、“以后提醒我”等操作。
AlertDialog本身方法:
dialog.cancle() 取消窗口,会执行DialogInterface.OnCancelListener
dialog.dismiss() 取消窗口,会执行DialogInterface.OnDismissListener
以上两种方法都是关闭对话框,只是调用的监听器不同
设置列表选项方法:
setItems,设置普通的列表
setSingleChoiceItems 设置带有单选按钮的列表
setMultiChoiceItems 设置带有多选按钮的列表
其他方法:
setIcon 设置对话框的图标
setCancelable设置点击对话框以外的地方是否可以关闭对话框,true表示可以
二、自定义Dialog
自定义Dialog有很多方法,最简单的就是创建一个类继承AlertDialog类
下面是我个人自定义的LoginDialog
package com.example.yougel.customdialogdemo;import android.app.AlertDialog;import android.content.Context;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.TextView;import android.widget.Toast;public class LoginDialog extends AlertDialog { public Context context; boolean isshow; //构造方法 protected LoginDialog(Context context) { super(context); this.context=context; } //设置是否显示标题 public void setShowTitle(boolean isshow){ this.isshow=isshow; } //登录监听接口 public interface onLoginListener { void onLogin(View v); } //监听器对象 onLoginListener monLoginListener; //设置登录监听器 public void setOnLoginListener(onLoginListener onLoginListener ){ monLoginListener=onLoginListener; } //显示LoginDialog public void show(){ View view=View.inflate(context,R.layout.login_dialog,null); AlertDialog.Builder builder=new AlertDialog.Builder(context); builder.setView(view); if(isshow){ TextView title= (TextView) view.findViewById(R.id.login_title); title.setText("用户登录"); } final CheckBox agree= (CheckBox) view.findViewById(R.id.agree); final Button login= (Button) view.findViewById(R.id.login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(agree.isChecked()){ monLoginListener.onLogin(login); }else{ Toast.makeText(context,"请选择已经阅读用户协议",Toast.LENGTH_SHORT).show(); } } }); builder.create().show(); }}
在Activity中的调用:
public void createLoginDialog(){ LoginDialog loginDialog=new LoginDialog(getContext()); loginDialog.setShowTitle(false); loginDialog.setOnLoginListener(new LoginDialog.onLoginListener() { @Override public void onLogin(View v) { Toast.makeText(getContext(),"登录",Toast.LENGTH_SHORT).show(); } }); loginDialog.show(); }
我在自定义的Dialog类中添加了一个自定义的监听器
(1)创建一个自定义监听接口;
(2)定义一个自定义监听接口对象;
(3)定义一个设置监听接口的方法;
(4)在自定义的Dialog中使用监听接口对象调用接口的方法。(我这里就是点击按钮时调用)
以下是LoginDialog的布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:gravity="center" android:id="@+id/login_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/font_color" android:textSize="26sp" android:background="@drawable/login_title_style" /> <EditText android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名" android:textColorHint="@color/hint" android:id="@+id/account" /> <EditText android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/password" android:textColorHint="@color/hint" android:hint="密码"/> <CheckBox android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/agree" android:text="已经阅读用户协议" /> <Button android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:layout_width="match_parent" android:layout_height="35dp" android:textColor="@color/font_color" android:id="@+id/login" android:background="@drawable/btn_style" android:text="登录"/></LinearLayout>
最后的界面:
- Android UI 之 Dialog
- Android UI学习之Dialog
- Android UI之对话框(dialog)
- 【Android】之【UI篇---Dialog(对话框)】
- Android之UI--打造万能自定义Dialog
- Android笔记 - UI Dialog
- Android-UI:Activity Dialog
- Android UI Dialog 使用
- android ui ->>Dialog对话框
- Android UI ------ Dialog
- Android UI基础之Dialog对话框的使用
- Android之UI--打造12种Dialog对话框
- Android UI——Dialog
- Android UI系列-----Dialog对话框
- Android UI系列-----Dialog对话框
- Android UI系列-----Dialog对话框
- Android UI设计:自定义Dialog
- Android UI系列-----Dialog对话框
- 世界各国的手机号码
- 我想成为一名产品经理
- win10菜单栏打不开的问题
- Spring中事物管理
- 存储过程详解
- Android UI 之 Dialog
- 升级ubuntu中的gcc和g++版本
- 序
- php curl后json_decode无法显示的问题
- JavaWeb—jsp页面中文乱码的几个问题
- 通配符与正则表达式
- Java实现选择排序
- python中os.system()的返回值
- Hibernate 双向一对多表的关联