自定义有两种方式
---注:在自定义中的控件监听都是 View下的 eg: button: new View.onClickListener, 需要声明,否则系统不能辨别
1. 用builder.setView(inflate.inflate(R.layout.myalertdialog_layout),null);
通过 id 实例化需要的控件,加监听,下同
缺点: 系统自动有个四边形窗体,当自定义对话框的背景为圆角时,四角为白的,不能被覆盖
2.写一个Activity 继承 AlertDialog, 将自定义的布局setcontentView() 重写在oncreate方法中V
View 的实例化及监听就加在Activity里,
需要的时候,直接 new AlertDialogActivity (Context context).show();
//调用
new DefinedAlertDialog(DialogActivity.this).show();
public class DefinedAlertDialog extends AlertDialog {
private Button confirmBtn, cancelBtn;
Context context;
protected DefinedAlertDialog(Context context) {
super(context);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_alert_d_layout);
initButton(); //初始化View
setListener(); //加监听
}
private void initButton() {
confirmBtn = (Button) findViewById(R.id.button2);
cancelBtn = (Button) findViewById(R.id.button1);
}
private void setListener() {
confirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/bg_review_tag_onpress" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title:提示"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="are you sure to quite"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="confirm"
android:background="@drawable/button_click_selector" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="cancel"
android:background="@drawable/button_click_selector"/>
</LinearLayout>
</LinearLayout>