Computer >> Máy Tính >  >> Lập trình >> Android

Làm cách nào để triển khai Chế độ xem AlertDialog tùy chỉnh trong Android?

Ví dụ này trình bày cách triển khai Chế độ xem AlertDialog tùy chỉnh trong Android.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

    

Bước 3 - Thêm mã sau vào res / layout / my_dialog.xml.

          

Bước 4 - Thêm mã sau vào res / drawable / button_background.xml.

           

Bước 5 - Thêm mã sau vào res / drawable / ic_success.xml.

   <đường dẫn android:fillColor ="# FFFFFF" android:pathData ="M468.907,214.604c-11.423,0 -20.682,9.26 - 20.682,20.682v20.831c-0.031,54.338 -21.221.105.412 -59.666.143.812c38.417,38.372 -89.467,59,5 -143.761,59,5c-0,04,0 -0,08,0 -0,12,0C132.506.459.365 41.3.368.056 41.364 , 255.883c0.031, -54.337 21.221, -105.411 59.667, -143.813c38.417, - 38.372 89.468, -59.5 143.761, -59.5c0.04,0 0,08,0 0,12,0c28.672,0.016 56,49,5.942 82,68, 17.611c10.436,4.65 22.659, -0.041 27.309, -10.474c4.648, -10.433 -0.04, -22.659 - 10.474, -27.309c-31.516, -14.043 -64.989, -21.173 -99.492, -21.192c-0.052, 0 -0.092,0 - 0,144,0c-65.329,0 -126.767,25.428 -172.993,71,6C25.536.129.014 0,038.190.473 0,255.861c-0,037,65.386 25.389.126.874 71.599, 173.136c46.21.46.262 107.668,71,76 173,05 5,71,798c0,051,0 0,092,0 0,144,0c65.329,0 126,767, -25,427 172,993, -71,6c46.262, -46.20971,76, -107,66871,798, -173,066v20,842C489,589, 223,864 480,33 , 214.604 468.907,214.604z "/> <đường dẫn android:fillColor =" # FFFFFF "android:pathData =" M505.942,39.803c-8.077, - 8.076 -21.172, -8.076 -29.249,0L244.794,271.701l-52.609, -52.609c-8.076, -8.077 -21.172, -8.077 -29.248,0c-8.077,8.077 -8.077,21.172 0,29.249l67.234,67.234c4.038,4.0399.332,6.058 14.625,6.058c5.293,0 10.586, -2.019 14.625, -6.058L505.942,69.052C514.019,60.975 514.019,47.88 505.942,39.803z "/>  

Bước 6 - Thêm mã sau vào src / MainActivity.java

 package com.app.sample; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; nhập android.os.Bundle; nhập android.os.Bundle; nhập android.view.LayoutInflater; nhập android.view.View; import android.view.ViewGroup; public class MainActivity mở rộng AppCompatActivity {@Override được bảo vệ void onCreate (Gói đã lưuInstanceState) {super.onCreate (saveInstanceState); setContentView (R.layout.activity_main); findViewById (R.id.buttonShowDialog) .setOnClickListener (new View.OnClickListener () {@Override public void onClick (View v) {showCustomDialog ();}}); } private void showCustomDialog () {ViewGroup viewGroup =findViewById (android.R.id.content); Xem hộp thoạiView =LayoutInflater.from (this) .inflate (R.layout.my_dialog, viewGroup, false); AlertDialog.Builder builder =new AlertDialog.Builder (this); builder.setView (Hộp thoại); AlertDialog alertDialog =builder.create (); alertDialog.show (); }} 

Bước 7 - Thêm mã sau vào Manifests / AndroidManifest.xml

            

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ studio android, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Run từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm cách nào để triển khai Chế độ xem AlertDialog tùy chỉnh trong Android?