自定义布局对话框的使用(Dialog)

1、对话框在平时比较常用,实现起来也比较简单。下面是自定义布局对话框的代码:

public void showDialog(){		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		LayoutInflater inflater = getLayoutInflater();
	    final View layout = inflater.inflate(R.layout.dialog, null);//获取自定义布局
	    builder.setView(layout);
	    builder.setIcon(R.drawable.ic_launcher);//设置标题图标
		builder.setTitle(R.string.hello_world);//设置标题内容
		//builder.setMessage("");//显示自定义布局内容
		final AnalogClock clock = (AnalogClock)layout.findViewById(R.id.clock);
		
		Button button = (Button)layout.findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				clock.setBackgroundResource(R.drawable.ic_launcher);
				Toast.makeText(getApplication(), "button", Toast.LENGTH_SHORT).show();
			}
		});		
		//确认按钮
		builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplication(), "ok", Toast.LENGTH_SHORT).show();					
			}
		});
		//取消
		builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				
			}
		});
		final AlertDialog dlg = builder.create();
		dlg.show();
	 }
2、实现的布局代码:
<?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" >
    
	<AnalogClock
	    android:id="@+id/clock"
	    android:layout_gravity="center_horizontal"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"/>
	<Button 
	    android:id="@+id/button"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"/>
</LinearLayout>




  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值