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

Làm cách nào để tạo hộp thoại tùy chỉnh toàn màn hình trong Android?

Ví dụ này minh họa về Cách tạo hộp thoại tùy chỉnh toàn màn hình.

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.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:id="@+id/parent"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity"
   android:gravity="center"
   android:orientation="vertical">
   <Button
      android:id="@+id/customDialog"
      android:text="Custom Dialog"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã sử dụng nút. Khi người dùng nhấp vào nút, nó sẽ hiển thị hộp thoại tùy chỉnh.

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

package com.example.andy.myapplication;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      findViewById(R.id.customDialog).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Rect displayRectangle = new Rect();
            Window window = MainActivity.this.getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
            final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.CustomAlertDialog);
            ViewGroup viewGroup = findViewById(android.R.id.content);
            View dialogView = LayoutInflater.from(v.getContext()).inflate(R.layout.customview, viewGroup, false);
            dialogView.setMinimumWidth((int)(displayRectangle.width() * 1f));
            dialogView.setMinimumHeight((int)(displayRectangle.height() * 1f));
            builder.setView(dialogView);
            final AlertDialog alertDialog = builder.create();
            Button buttonOk=dialogView.findViewById(R.id.buttonOk);
            buttonOk.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                  alertDialog.dismiss();
               }
            });
            alertDialog.show();
         }
      });
   }
}

Để hiển thị hộp thoại tùy chỉnh, chúng tôi đã tăng chế độ xem dưới dạng chế độ xem tùy chỉnh. vì vậy hãy tạo customview.xml và thêm mã sau -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">
   <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:padding="16dp">
      <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Success"
         android:textAlignment="center"
         android:textAppearance="@style/TextAppearance.AppCompat.Headline" />
      <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="10dp"
         android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eu erat tincidunt lacus fermentum rutrum."
         android:textAlignment="center"
         android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
      <Button
         android:id="@+id/buttonOk"
         android:layout_width="200dp"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:layout_marginTop="15dp"
         android:background="@color/colorPrimary"
         android:text="Ok"
         android:textColor="#FFF" />
   </LinearLayout>
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã triển khai chủ đề tùy chỉnh cho hộp thoại cảnh báo như được hiển thị bên dưới -

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.CustomAlertDialog);

Bây giờ, hãy tạo một thẻ trong res / styles.xml như hình dưới đây -

<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
   <item name="android:windowBackground">@drawable/popup_background</item>
</style>

Trong cod ở trên, chúng tôi đã thêm nền là popup_backround, vì vậy hãy tạo một bản vẽ có thể trong thư mục có thể vẽ dưới dạng popup_background.xml và thêm đoạn mã sau-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
   <solid android:color="#FFFFFF" />
   <corners android:radius="6dp" />
</shape>

Để hiển thị hộp thoại cảnh báo tùy chỉnh toàn màn hình, hãy sử dụng mã sau -

Rect displayRectangle = new Rect();
Window window = MainActivity.this.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.CustomAlertDialog);
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(v.getContext()).inflate(R.layout.customview, viewGroup, false);
dialogView.setMinimumWidth((int)(displayRectangle.width() * 1f));
dialogView.setMinimumHeight((int)(displayRectangle.height() * 1f));
builder.setView(dialogView);
final AlertDialog alertDialog = builder.create();
Button buttonOk=dialogView.findViewById(R.id.buttonOk);
buttonOk.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      alertDialog.dismiss();
   }
});
alertDialog.show();

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 Runicon 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 để tạo hộp thoại tùy chỉnh toàn màn hình trong Android?

Trong kết quả trên, nó hiển thị màn hình ban đầu. Bây giờ nhấp vào nút nó sẽ mở hộp thoại tùy chỉnh với toàn màn hình như hình dưới đây -

Làm cách nào để tạo hộp thoại tùy chỉnh toàn màn hình trong Android?