创建自定义AlertDialog

没什么特别的地方,只是自定义弹出一个简单的对话框。
效果图:

创建自定义AlertDialog_第1张图片
创建自定义AlertDialog_第2张图片
创建自定义AlertDialog_第3张图片

首先定义一个布局文件 custom_dialog.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:background="@drawable/stroke">
    <TextView
        android:id="@+id/dialog_msg"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:textColor="@color/colorPrimary"
        android:textSize="18sp"
        android:text="确认是否发布?"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="40dp"
        />
    <View
        android:layout_width="200dp"
        android:layout_height="1dp"
        android:background="@color/colorPrimaryDark"/>
    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/cancel_btn"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="18sp"
            android:gravity="center"
            android:text="取消"
            />
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"/>
        <TextView
            android:id="@+id/yes_btn"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="18sp"
            android:gravity="center"
            android:text="确定"
            android:textColor="#000"
            />
    LinearLayout>

LinearLayout>

创建CustomDialog继承Dialog,需要重写构造方法,这里我们在super()中把Theme写死,避免每次都要传入样式,不过也可以更灵活地把Theme放在构造方法的参数中,每次使用弹窗可引不同的style,也可以把要填充的布局文件放入构造方法的参数中传递进来,不过这样就需要再定义不同的控件响应时间(因为引入不同的布局文件,那么控件类型和ID肯定也会有变化了)
style文件dialog.xml:

CustomDialog.java文件:

/**
 * Created by Commy on 2016/8/23.
 */
public abstract class CustomDialog extends Dialog{
    private Context mContext;
    private String msg;

    public CustomDialog(Context context,String msg) {
        super(context,R.style.dialog);
        mContext = context;
        this.msg = msg;
        /*
            填充自定义弹窗布局文件
         */
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.custom_dialog,null);
//        获取提示内容TextView控件:
        TextView message = (TextView)layout.findViewById(R.id.dialog_msg);
        message.setText(this.msg);
        setContentView(layout);
        bindLayoutClick(layout);
        this.show();
    }


    /**
     * 设置按钮点击相应:
     * @param layout
     */
    public void bindLayoutClick(View layout){
        TextView btn_yes = (TextView)layout.findViewById(R.id.yes_btn),
                btn_no = (TextView)layout.findViewById(R.id.cancel_btn);
        btn_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickYes();
                dismiss();
            }
        });
        btn_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickNo();
                dismiss();
            }
        });


    }


    /*
           定义按钮点击事件:
     */
    public abstract void clickYes();
    public abstract void clickNo();
}

最后在活动界面就可以定义一个按钮弹窗啦~

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button alertDialog = (Button)findViewById(R.id.alert);
        alertDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomDialog customDialog = new CustomDialog(MainActivity.this,"这是个弹窗") {
                    @Override
                    public void clickYes() {
                        showToast("点击了positive");
                    }

                    @Override
                    public void clickNo() {
                        showToast("点击了negative");
                    }
                };
            }
        });
    }

    public void showToast(String msg){
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
}

至此,自定义AlertDialog的步骤就完成了,完整代码:

自定义Alertdialog完整代码,点我~

你可能感兴趣的:(android,对话框,自定义,AlertDialo,android,自定义控件)