自定義 Dialog

weixin_34321977發表於2018-02-01

/**

  • Created by JsonQiu on 2018/2/1.
    */

public class MyDialog extends Dialog {

private final Float fHeight;
private final Float fWeight;
private Activity context;
private String title;
private String confirmButtonText;
private String cacelButtonText;
private ClickListenerInterface clickListenerInterface;

public interface ClickListenerInterface {

    public void doConfirm();

    public void doCancel();
}

public MyDialog(Activity context, String title, String confirmButtonText, String cacelButtonText,Float fHeight,Float fWeight) {
    super(context, R.style.mydialog);
    this.context = context;
    this.title = title;
    this.confirmButtonText = confirmButtonText;
    this.cacelButtonText = cacelButtonText;
    this.fHeight = fHeight;
    this.fWeight = fWeight;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    init();
}

public void init() {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.my_self_dialog, null);
    setContentView(view);

    TextView tvTitle = (TextView) view.findViewById(R.id.my_dialog_tv_title);
    Button btnCancle = (Button) view.findViewById(R.id.my_dialog_btn_cancel);
    Button btnConfirm = (Button) view.findViewById(R.id.my_dialog_btn_confirm);

    tvTitle.setText(title);
    btnConfirm.setText(confirmButtonText);
    btnCancle.setText(cacelButtonText);

    btnCancle.setOnClickListener(new clickListener());
    btnConfirm.setOnClickListener(new clickListener());

    Window dialogWindow =getWindow();
    WindowManager m = context.getWindowManager();
    Display d = m.getDefaultDisplay();
    WindowManager.LayoutParams p = dialogWindow.getAttributes();
    p.height = (int) (d.getHeight() * fHeight);
    p.width = (int) (d.getWidth() * fWeight);
    dialogWindow.setAttributes(p);
}

public void setClicklistener(ClickListenerInterface clickListenerInterface) {
    this.clickListenerInterface = clickListenerInterface;
}

private class clickListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.my_dialog_btn_cancel:
                clickListenerInterface.doCancel();
                break;
            case R.id.my_dialog_btn_confirm:
                clickListenerInterface.doConfirm();
                break;
        }
    }

}

}

<style name="mydialog" >
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowActionModeOverlay">true</item>
</style>

<?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:background="@drawable/bg_shap"
android:orientation="vertical">

<TextView
    android:id="@+id/my_dialog_tv_title"
    style="@style/MatchWrap"
    android:layout_marginTop="@dimen/dp120"
    android:gravity="center"
    android:text=" "
    android:textColor="@color/grey_color2"
    android:textSize="@dimen/txt_48" />

<LinearLayout
    style="@style/MatchWrap"
    android:layout_marginTop="@dimen/dp100"
    android:gravity="center_horizontal"
    android:orientation="horizontal">

    <Button
        android:id="@+id/my_dialog_btn_cancel"
        android:layout_width="@dimen/dp200"
        android:layout_height="@dimen/dp80"
        android:background="@drawable/lock_quxiao"
        android:text=" "
        android:textColor="@color/bg_blue"
        android:textSize="@dimen/txt_32" />

    <Button
        android:id="@+id/my_dialog_btn_confirm"
        android:layout_width="@dimen/dp200"
        android:layout_height="@dimen/dp80"
        android:layout_marginLeft="50dp"
        android:background="@drawable/lock_queren"
        android:text=" "
        android:textColor="@color/white"
        android:textSize="@dimen/txt_32" />
</LinearLayout>

</LinearLayout>

bg_shap

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white" />
<corners android:radius="12dp" />
</shape>

final MyDialog myDialog = new MyDialog(getActivity(), "確認刪除此稱重商品?", "確認", "取消", 0.4f, 0.45f);
myDialog.setCanceledOnTouchOutside(true);
//設定彈出框失去焦點是否隱藏,即點選遮蔽其它地方是否隱藏
myDialog.show();
myDialog.setClicklistener(new MyDialog.ClickListenerInterface() {
@Override
public void doConfirm() {
myDialog.dismiss();
}

@Override
public void doCancel() {
    Toast.makeText(getActivity(), "取消", Toast.LENGTH_SHORT).show();
    myDialog.dismiss();
}

});

相關文章