自定義 Dialog
/**
- 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();
}
});
相關文章
- flutter:教你自定義DialogFlutter
- 自定義dialog樣式
- Flutter 23: 圖解自定義 Dialog 對話方塊Flutter圖解
- Android 最簡單的自定義Dialog之一Android
- Android自定義View(1):對話方塊-DialogAndroidView
- (轉)Android 自定義Dialog實現步驟及封裝Android封裝
- C++ Qt開發:自定義Dialog對話方塊元件C++QT元件
- C/C++ Qt 自定義Dialog對話方塊元件應用C++QT元件
- Android獲取dialog自定義佈局中的控制元件Android控制元件
- 【Flutter 專題】124 日常問題小結 (三) 自定義 Dialog 二三事Flutter
- Alert Dialog "Done"按鈕定義.
- 自定義View:自定義屬性(自定義按鈕實現)View
- 使用 vue3 的自定義指令給 element-plus 的 el-dialog 增加拖拽功能Vue
- Android 自定義dialog,實現右上角顯示一個控制元件按鈕Android控制元件
- 使用DialogFragment定義自己的DialogFragment
- 08.Django自定義模板,自定義標籤和自定義過濾器Django過濾器
- 自定義ImageView完成圓形頭像自定義View
- 自定義VIEWView
- 自定義圓環
- 自定義SnackBar
- 自定義useState
- 自定義_ajax
- 自定義Annotation
- 自定義OrderedMap
- 自定義 Drawable
- 自定義UICollectionViewLayoutUIView
- 自定義UITabBarUItabBar
- 自定義scrollbar
- 自定義Drawable
- 自定義ToastAST
- 自定義吐司
- 自定義 tabBartabBar
- 自定義表格
- 自定義 GitGit
- tailwind自定義AI
- android自定義view(自定義數字鍵盤)AndroidView
- vue自定義全域性元件(或自定義外掛)Vue元件
- android自定義View&自定義ViewGroup(下)AndroidView