AlertDialog 使用 扩展

AlertDialog 使用 扩展


2024年3月14日发(作者:)

AlertDialog

[功能]

也是一种Dialog

[原理]

1. AlertDialog 本身并没有构造函数 即 不可以通过 new AlertDialog(...) 来初始化 而只能

通过 r

2. 而 r 比较像是AlertDialog的构造器 用于接收各种和 AlertDialog 有

关的参数 然后通过 create() 来创建目标 AlertDialog

[代码 步骤]

1. 定义 r 实例 并接受一些参数 如:图片 标题 正文

Java代码

 ab = new r(this);

Java代码

 le("HelloAlert").setMessage("Warning: its Alert

Demo!").setIcon();

2. 根据r 创建 相应的 AlertDialog

Java代码

 aDialog = ();

3. 弹出 AlertDialog

Java代码

findViewById().setOnClickListener(new OnClickListener(){

public void onClick(View v) {

// TODO Auto-generated method stub

();

}

});

4. 取消 AlertDialog

写道

因为该AlertDialog 所采用的布局是系统的 其只接受Text 不接受Button 但是发现可以在

AlertDialog 上面注册按键监听 即eyListener()

不过因为该监听器只负责监听按键事件 而鼠标点击是不管的 所以请点击任意按键关闭之

Java代码























eyListener(new OnKeyListener(){

@Override

public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {

// TODO Auto-generated method stub

s();

return true;

}

});

* emulator 运行截图:

5. 以上所采用的都是AlertDialog 系统默认的布局 现在说自定义布局的情况 并添加一个

用于取消AlertDialog 的 Button

* 定义其布局

Xml代码





 android:orientation="horizontal"

 android:layout_width="fill_parent"

 android:layout_height="fill_parent"

 android:padding="10dp"

 >



 android:id="@+id/image"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:src="@drawable/robot" />



 android:orientation="vertical"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 >



 android:id="@+id/title"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="HelloAlert!"

 />



 android:id="@+id/message"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:paddingTop="10dip"

 />





* 通过LayoutInflater 得到上面 布局的 View view

Java代码

 view = outInflater().inflate(, null);

* 指定r 所需的布局 并返回目标AlertDialog

Java代码

 w(view);



 aDialog = ();

* 通过 ewById() 得到 目标View 然后设置其内容 如:

Java代码

 TextView title = (TextView) ewById();

 tSize(20);

 tColor();

 t("HelloAlert");



 TextView message = (TextView) ewById(e);

 t("Warning: it's Alert Demo!");

* 弹出 AlertDialog

Java代码

 findViewById().setOnClickListener(new OnClickListener(){

 public void onClick(View v) {

 // TODO Auto-generated method stub

 ();

 }

 });

* 取消 AlertDialog

写道

在整个View 上注册按键监听器 关闭AlertDialog

Java代码

 lickListener(new OnClickListener(){

 public void onClick(View v) {

 // TODO Auto-generated method stub

 s();

 }

 });

* emulator 运行截图:


发布者:admin,转转请注明出处:http://www.yc00.com/web/1710364899a1744341.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信