主方法中只是调用
private void showpop(View v ){
View pv = View.inflate(this,R.layout.popwindow, null);
TextView tv_photo = (TextView)pv.findViewById(R.id.tv_msg);
final PopupWindow pop = new PopupWindow(pv, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
pop.setOutsideTouchable(true);
pop.setBackgroundDrawable(new ColorDrawable(0*00000000));
pop.setFocusable(true);
tv_photo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"拍照",Toast.LENGTH_SHORT).show();
pop.dismiss();
}
});
}
重新写一个类
private Context context;
private String msg ;
private String btnConfirm ;
private String btnCancel ;
private TextView tv_msg ;
private Button btn_confirm ;
private Button btn_cancel ;
public MyDialig(Context context,String msg,String btnConfirm,String btnCancel) {
super(context,R.style.mydialog);
// TODO Auto-generated constructor stub
this.context = context;
this.msg = msg ;
this.btnCancel = btnCancel;
this.btnConfirm = btnConfirm;
}
private MyClickListener listener ;
创建一个接口
public interface MyClickListener{
public void confirm() ;
public void cancel() ;
}
给接口赋值
public void setClickListener (MyClickListener listener){
this.listener = listener;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text1);
tv_msg = (TextView)findViewById(R.id.tv_msg);
btn_confirm =(Button)findViewById(R.id.btn_confirm);
btn_cancel = (Button)findViewById(R.id.btn_cancel);
tv_msg.setText(msg);
btn_cancel.setText(btnCancel);
btn_confirm.setText(btnConfirm);
btn_cancel.setOnClickListener(this);
btn_confirm.setOnClickListener(this);
设置Dialog大小
Window windows = getWindow();
WindowManager主要用来管理窗口的一些状态、属性
WindowManager wm = ((Activity)context).getWindowManager();
获取当前对话框的参数值
设置和获得当前窗口的一些属性
WindowManager.LayoutParams params =windows.getAttributes();
设置窗口显示的位置
windows.setGravity(Gravity.CENTER);
获取屏幕的宽高
Display display = wm.getDefaultDisplay();
把屏幕的宽度设为0.6
params.width = (int) (display.getWidth()*0.6);
windows.setAttributes(params);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
触发主方法中的 方法
switch (v.getId()) {
case R.id.btn_confirm:
listener.confirm();
dismiss();
break;
case R.id.btn_cancel:
listener.cancel();
dismiss();
break;
}
}
}
网友评论