Android學習筆記3-4
推薦新手向學習視頻:B站https://www.bilibili.com/video/av38409964點我傳送
3-4 自定義Dialog
-
layout_custom_dialog.xml文件
<?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:gravity="center_horizontal" android:orientation="vertical" android:background="@drawable/bg_custom_dialog"> <!--這裏改成透明的了,否則最上方會出現空白,也可以不加背景色--> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提示" android:textColor="@color/colorBlack" android:textStyle="bold" android:textSize="20sp" /> <TextView android:id="@+id/tv_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="確認刪除此項?" android:textColor="@color/colorBlack" android:textSize="20sp" android:layout_marginTop="20dp" android:layout_marginBottom="20dp"/> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/colorBlack"/> <LinearLayout android:layout_width="match_parent" android:layout_height="45dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_cancel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="取消" android:textSize="20sp" android:textColor="#0FA8CE" android:gravity="center"/> <View android:layout_width="0.5dp" android:layout_height="match_parent" android:background="@color/colorBlack"/> <TextView android:id="@+id/tv_confirm" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="確定" android:textSize="20sp" android:textColor="#0FA8CE" android:gravity="center"/> </LinearLayout> </LinearLayout>
-
bg_custom_dialog.xml文件設置圓角
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/colorTransparent"/> <corners android:radius="10dp"/> </shape>
-
CustomDialog.java文件
package com.ylw.helloworld.widget; import android.app.Dialog; import android.content.Context; import android.graphics.Point; import android.os.Bundle; import android.text.TextUtils; import android.view.Display; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.ylw.helloworld.R; public class CustomDialog extends Dialog implements View.OnClickListener { private TextView mTvTitle,mTvMessage,mTvCancel,mTvConfirm; private String title,message,cancel,confirm; private IOCancelListener cancelListener; private IOConfirmListener confirmListener; public CustomDialog(@NonNull Context context) { super(context); } public CustomDialog(@NonNull Context context, int themeResId) { super(context, themeResId); } public CustomDialog setTitle(String title) { this.title = title; return this; } public CustomDialog setMessage(String message) { this.message = message; return this; } public CustomDialog setCancel(String cancel,IOCancelListener listener) { this.cancel = cancel; this.cancelListener = listener; return this; } public CustomDialog setConfirm(String confirm,IOConfirmListener listener) { this.confirm = confirm; this.confirmListener = listener; return this; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_custom_dialog); //設置寬度,低版本可能會顯示不正常需要設置,目前我用的不需要設置 // WindowManager m = getWindow().getWindowManager(); // Display d = m.getDefaultDisplay(); // WindowManager.LayoutParams p = getWindow().getAttributes(); // Point size = new Point(); // d.getSize(size); // p.width = (int) (size.x * 0.8);//設置dialog的寬度爲當前手機屏幕寬度的80% // getWindow().setAttributes(p); mTvTitle = findViewById(R.id.tv_title); mTvMessage = findViewById(R.id.tv_message); mTvCancel = findViewById(R.id.tv_cancel); mTvConfirm = findViewById(R.id.tv_confirm); if (!TextUtils.isEmpty(title)){ mTvTitle.setText(title); } if (!TextUtils.isEmpty(message)){ mTvTitle.setText(message); } if (!TextUtils.isEmpty(cancel)){ mTvTitle.setText(cancel); } if (!TextUtils.isEmpty(confirm)){ mTvTitle.setText(confirm); } mTvCancel.setOnClickListener(this); mTvConfirm.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.tv_cancel: if (cancelListener != null){ cancelListener.onCancel(this); } dismiss(); break; case R.id.tv_confirm: if (confirmListener != null){ confirmListener.onConfirm(this); } dismiss(); break; } } public interface IOCancelListener{ void onCancel(CustomDialog dialog); } public interface IOConfirmListener{ void onConfirm(CustomDialog dialog); } }
-
CustomDialogActivity
package com.ylw.helloworld; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.ylw.helloworld.util.ToastUtil; import com.ylw.helloworld.widget.CustomDialog; public class CustomDialogActivity extends AppCompatActivity { private Button mBtnDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_dialog); mBtnDialog = findViewById(R.id.btn_custom_dialog); mBtnDialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CustomDialog customDialog = new CustomDialog(CustomDialogActivity.this); customDialog.setTitle("提示").setMessage("確認刪除此項?") .setCancel("取消", new CustomDialog.IOCancelListener() { @Override public void onCancel(CustomDialog dialog) { ToastUtil.showMsg(CustomDialogActivity.this,"cancel..."); } }).setConfirm("確認", new CustomDialog.IOConfirmListener() { @Override public void onConfirm(CustomDialog dialog) { ToastUtil.showMsg(CustomDialogActivity.this,"confirm..."); } }).show(); } }); } }
-
效果