android dialog常见的几种用法

一、 系统标准对话框,效果如下:

对于这种标准的dialog,我们只需要吧相应的属性值填好即可。部分代码如下:

//创建第一类对话框,标准对话框
	public Dialog builderDialog1(){
		AlertDialog.Builder factory = new AlertDialog.Builder(this);
                //相信大家能看懂下面的信息
                factory.setTitle("删除提示");
		factory.setMessage("是否删除?");
		factory.setIcon(R.drawable.icon);
		//相应两个按钮的事件
		factory.setPositiveButton("删除", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(ActivityDialog.this, "删除按钮被点击", Toast.LENGTH_LONG).show();					
			}
		});

                //相应两个按钮的事件

  factory.setNegativeButton("取消", new DialogInterface.OnClickListener() 
{public void onClick(DialogInterface dialog, int which) 
{Toast.makeText(ActivityDialog.this, "取消按钮被点击", Toast.LENGTH_LONG).show();}});return factory.create();}

二、自定义视图dialog,  这类dialog可以在里面添加控件,效果如下:

自定义dialog 需要借助视图, XML代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:orientation="vertical"  
  android:layout_width="fill_parent" 
   android:layout_height="fill_parent"    >
<TextView    android:id="@+id/tvinfo" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content" 
   android:text="请输入密码"/>
<EditText    android:id="@+id/etpwd" 
   android:layout_width="match_parent" 
   android:layout_height="wrap_content"/>
<Button     android:id="@+id/bok"  
  android:layout_width="match_parent" 
   android:layout_height="wrap_content"  
  android:text="submit"   ></Button></LinearLayout>
java代码如下:
//创建第二类对话框,对话框上的视图自定义 
public Dialog builderDialog2()
{ LayoutInflater inflater = getLayoutInflater();
 View dialog_view = inflater.inflate(R.layout.dialogview, null); 
Button bok = (Button) dialog_view.findViewById(R.id.bok);
 final EditText etpwd = (EditText) dialog_view.findViewById(R.id.etpwd);
 bok.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {    Toast.makeText(ActivityDialog.this, "OK按钮被点击", Toast.LENGTH_LONG).show();  
  setTitle("对话框上输入文本是:"+etpwd.getText());  
  //关闭指定id的对话框  
  dismissDialog(dialog_id2); } }); 
AlertDialog.Builder facotry = new AlertDialog.Builder(this);
 facotry.setView(dialog_view); return facotry.create(); }


三、为进度对话框, 这个对话框的特点是带进度条, 效果如下:

这类对话框,使用ProgressDialog即可, java代码如下:
//创建第三类对话框,进度对话框
public Dialog builderDialog3(){
ProgressDialog pro_dialog = new ProgressDialog(this);
 pro_dialog.setMessage("正在下载....");
return pro_dialog; }


这些都是dialog常见,且基础部分。 如果大家有什么疑问,请留言。 完整代码:http://115.com/file/c2oj9bci#dialog.rar





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值