自定义布局对话框的使用(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、实现的布局代码:


    
	
	

自定义布局对话框的使用(Dialog)_第1张图片



你可能感兴趣的:(android)