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

Làm cách nào để hiển thị hộp thoại cảnh báo trên Android?

Trước khi vào hộp thoại cảnh báo, chúng ta nên biết hộp thoại cảnh báo là gì, hộp thoại cảnh báo giống như một cửa sổ bật lên nơi người dùng có thể chọn hành động bằng cách nhấp vào nút "ok" hoặc "hủy".

Các phương thức trong Hộp thoại Cảnh báo

  • setView (Chế độ xem) - Nó được sử dụng để đặt chế độ xem tùy chỉnh thành hộp thoại cảnh báo

  • setTitle (tiêu đề CharSequence) - Nó được sử dụng để đặt tiêu đề cho hộp thoại cảnh báo

  • setMessage (thông báo CharSequence) - Nó là cuộc gọi đơn giản như nội dung trong hộp cảnh báo

  • setIcon (int resId) - nó được sử dụng để đặt biểu tượng cho hộp cảnh báo

  • setButton (int whichButton, CharSequence text, Message msg) - Nó được sử dụng để thiết lập nút cho hộp thoại cảnh báo như ví dụ minh họa bên dưới.

  • getListView () - nó được sử dụng để xem danh sách được sử dụng bên trong hộp thoại cảnh báo.

Ví dụ này minh họa về cách triển khai hộp thoại cảnh báo 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"?>
<android.support.constraint.ConstraintLayout
xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:app = "https://schemas.android.com/apk/res-auto"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
   <Button
      android:id = "@+id/button"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Click"
      app:layout_constraintBottom_toBottomOf = "parent"
      app:layout_constraintLeft_toLeftOf = "parent"
      app:layout_constraintRight_toRightOf = "parent"
      app:layout_constraintTop_toTopOf = "parent" />
</android.support.constraint.ConstraintLayout>

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

package com.example.andy.myapplication;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button button=findViewById(R.id.button);
      button.setOnClickListener(this);
   }
   @Override
   public void onClick(View v) {
      switch (v.getId()){
         case R.id.button:
         alertDialog();
         break;
      }
   }
   private void alertDialog() {
      AlertDialog.Builder dialog=new AlertDialog.Builder(this);
      dialog.setMessage("Please Select any option");
      dialog.setTitle("Dialog Box");
      dialog.setPositiveButton("YES",
      new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog,
         int which) {
            Toast.makeText(getApplicationContext(),"Yes is clicked",Toast.LENGTH_LONG).show();
         }
      });
      dialog.setNegativeButton("cancel",new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(),"cancel is clicked",Toast.LENGTH_LONG).show();
         }
      });
      AlertDialog alertDialog=dialog.create();
      alertDialog.show();
   }
}

Trong đoạn mã trên, chúng ta đã tạo một nút khi người dùng nhấp vào nút đó sẽ hiện ra hộp thoại cảnh báo, người dùng có thể chọn đồng ý hoặc hủy tùy theo yêu cầu.

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 cách nào để hiển thị hộp thoại cảnh báo trên Android?

Bây giờ nhấp vào nút trên nó sẽ hiển thị hộp thoại cảnh báo như hình dưới đây

Làm cách nào để hiển thị hộp thoại cảnh báo trên Android?

Bây giờ chọn nút có / hủy nó sẽ đưa ra kết quả như hình dưới đây

Làm cách nào để hiển thị hộp thoại cảnh báo trên Android?


Làm cách nào để hiển thị hộp thoại cảnh báo trên Android?