自定义View是程序员必备基础技能,今天就对Dialog进行自定义
自定义一个类继承Dialog
/**
*
Created by TangRen on 2016/1/12.
这里我写了一个登陆Dialog
*/
public class MyDialog extends Dialog {
private Button login,register;
private EditText user,psw;
public MyDialog(Context context) {
super(context,R.style.myDialog);
setDialog();
}
private void setDialog() {
View view= LayoutInflater.from(getContext()).inflate(R.layout.dialog_main,null);
login= (Button) view.findViewById(R.id.login);
register= (Button) view.findViewById(R.id.register);
user= (EditText) view.findViewById(R.id.user);
psw= (EditText) view.findViewById(R.id.psw);
setContentView(view);
}
public View getEditUser(){
return user;
}
public View getEditPsw(){
return psw;
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
}
@Override
public void setContentView(View view) {
super.setContentView(view);
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
super.setContentView(view, params);
}
自定义按钮的点击事件
public void setPostListener(View.OnClickListener listener){
login.setOnClickListener(listener);
}
public void setNegaListener(View.OnClickListener listener){
register.setOnClickListener(listener);
}
}
以下是Activity的处理
dialog=new MyDialog(this);
//对显示窗口做处理
Window window=dialog.getWindow();
Display display=getWindowManager().getDefaultDisplay();
WindowManager.LayoutParams params=window.getAttributes();
params.width= (int) (0.9*display.getWidth());
EditText user= (EditText) dialog.getEditUser();
EditText psw= (EditText) dialog.getEditPsw();
dialog.setPostListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Your own code
}
});
dialog.setNegaListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Your own code
}
});
dialog.show();//切记不要忘记