Dialog对话框之自定义AlertDialog

自定义有两种方式

---注:在自定义中的控件监听都是 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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值