一个自定义dialog

主要是用于dialog的内容变化但是部分内容不变复用方便写的

publicclassMainDialogextendsDialogimplementsView.OnClickListener{

privateViewroot;

privateViewGroupcontainer;

privateButtonbtnObtain;

privateIWheelPickerpicker;

privateStringdata;

publicMainDialog(Contextcontext) {

super(context);

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

setCanceledOnTouchOutside(true);

setOnDismissListener(newOnDismissListener() {

@Override

publicvoidonDismiss(DialogInterfacedialog) {

container.removeAllViews();

}

});

//我们为dialog设置一个布局文件

root=getLayoutInflater().inflate(R.layout.ac_main_dialog,null);

//跟布局里面的动态添加布局的容器

container=(ViewGroup) root.findViewById(R.id.main_dialog_container);

//公用的确认按钮

btnObtain=(Button) root.findViewById(R.id.btn_obtain);

btnObtain.setOnClickListener(this);

}

//复写他的setcontentview()方法,将我们自定义的布局添加到我们的跟容器中,这样可以在代码中灵活的设置了,

@Override

publicvoidsetContentView(Viewview) {

if(viewinstanceofIWheelPicker) {

picker=(IWheelPicker) view;

picker.setOnWheelChangeListener(newAbstractWheelPicker.SimpleWheelChangeListener() {

@Override

publicvoidonWheelScrollStateChanged(intstate) {

if(state!=AbstractWheelPicker.SCROLL_STATE_IDLE) {

btnObtain.setEnabled(false);

}else{

btnObtain.setEnabled(true);

}

}

@Override

publicvoidonWheelSelected(intindex,Stringdata) {

MainDialog.this.data=data;

}

});

}

container.addView(view);

super.setContentView(root);

}

@Override

publicvoidonClick(Viewv) {

switch(v.getId()) {

caseR.id.btn_obtain:

Toast.makeText(getContext(), data,Toast.LENGTH_SHORT).show();

break;

}

}

}

你可能感兴趣的:(一个自定义dialog)