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

Làm thế nào để tạo hộp thoại tùy chỉnh với các hành động xem hộp thoại tùy chỉnh trong Android?

Ví dụ này minh họa về Cách tạo hộp thoại tùy chỉnh với các hành động xem hộp thoại 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.

<?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.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.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) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            ViewGroup viewGroup = findViewById(android.R.id.content);
            View dialogView = LayoutInflater.from(v.getContext()).inflate(R.layout.customview, viewGroup, false);
            Button buttonOk=dialogView.findViewById(R.id.buttonOk);
            builder.setView(dialogView);
            final AlertDialog alertDialog = builder.create();
            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>

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ừ android studio, 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 Chạy 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 thế nào để tạo hộp thoại tùy chỉnh với các hành động xem hộp thoại tùy chỉ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 như hình dưới đây -

Làm thế nào để tạo hộp thoại tùy chỉnh với các hành động xem hộp thoại tùy chỉnh trong Android?

Bây giờ hãy nhấp vào nút ok, nó sẽ đóng hộp thoại cảnh báo.