自定义布局对话框的使用(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>

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

    原文作者:踏雪羽翼
    原文地址: https://blog.csdn.net/u011324501/article/details/51565084
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞