现在的位置: 首页 > 综合 > 正文

android 几种自定义 Dialog

2018年04月10日 ⁄ 综合 ⁄ 共 1389字 ⁄ 字号 评论关闭

方法一:

在Activity中直接使用:

private Context context; 
LayoutInflater factory = LayoutInflater.from(context);
View dialog = factory.inflate(R.layout.dialog, null);				//dialog的布局
TextView tv = (TextView) dialog.findViewById(R.id.textview1);			//注意用dialog.findViewById();
tv.setText("111");								//修改dialog布局中的信息
AlertDialog.Builder text = new AlertDialog.Builder(context);
text.setTitle("客户详细信息");
text.setView(dialog);
text.setPositiveButton("确定",new android.content.DialogInterface.OnClickListener() {
    		@Override
    		public void onClick(DialogInterface arg0, int arg1) {
		//添加确定后的操作
    		}
});
text.setNegativeButton("取消",null);
//AlerDialog.Builder才有确定、取消。
//虽然可以在自定义的dialog中使用Button ,但考虑整体效果还是使用setPositiveButton
text.show();



方法二:

1、自定义一个类,继承Dialog

<span style="white-space:pre">	</span>class CustomDialog extends Dialog {
		public CustomDialog(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}
		
		 protected void onCreate(Bundle savedInstanceState){
			 super.onCreate(savedInstanceState);
			 
			 setContentView(R.layout.dialog_feidan);
			 
			
			 Button button = (Button)findViewById(R.id.button);
			button.setOnClickListener(new Button.OnClickListener(){

					public void onClick(View v) {
						//按钮的操作
					}
		        });
		 }
		 
		 //called when this dialog is dismissed
		 protected void onStop() {
			 Log.d("TAG","+++++++++++++++++++++++++++");
		 }
		 
		 
<span style="white-space:pre">	</span>}

2、Activity中使用

<span style="white-space:pre">	</span>CustomDialog cd = new CustomDialog(context);   
	cd.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉dialog的标题
	cd.show();  </span>

抱歉!评论已关闭.