Trong Android, việc tạo đèn báo tùy chỉnh thường sử dụng lớp Dialog hoặc DialogFragment. Dưới đây là một ví dụ đơn giản về cách tạo đèn báo tùy chỉnh:
Xác định một tệp bố cục (ví dụ:
custom_popup.xml) để mô tả giao diện và hành vi của đèn báo.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp" android:background="@android:color/white"> <TextView android:id="@+id/popup_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tiêu đề" android:textSize="18sp" android:textStyle="bold"/> <TextView android:id="@+id/popup_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Thông điệp" android:paddingTop="10dp"/> <Button android:id="@+id/confirm_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Đồng ý"/> </LinearLayout>Tạo một lớp
DialogFragmenttùy chỉnh.public class CustomPopupFragment extends DialogFragment { public static CustomPopupFragment getInstance(String title, String message) { CustomPopupFragment popup = new CustomPopupFragment(); Bundle params = new Bundle(); params.putString("popupTitle", title); params.putString("popupMessage", message); popup.setArguments(params); return popup; } @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = requireActivity().getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_popup, null); builder.setView(layout) .setPositiveButton("Đồng ý", (dialog, id) -> { // Xử lý sự kiện khi nhấn nút Đồng ý }); TextView titleView = layout.findViewById(R.id.popup_title); TextView messageView = layout.findViewById(R.id.popup_message); Button confirmButton = layout.findViewById(R.id.confirm_button); titleView.setText(getArguments().getString("popupTitle")); messageView.setText(getArguments().getString("popupMessage")); return builder.create(); } }Gọi
CustomPopupFragmentở nơi cần hiển thị đèn báo.CustomPopupFragment popup = CustomPopupFragment.getInstance("Tiêu đề Của Tôi", "Thông điệp Của Tôi"); popup.show(getSupportFragmentManager(), "customPopup");
Bằng cách này, bạn đã tạo ra một đèn báo tùy chỉnh. Bạn có thể thêm các yếu tố tùy chỉnh khác như trường nhập liệu, hình ảnh, nút, v.v. dựa trên nhu cầu cụ thể.