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

Làm thế nào để tạo một hộp thoại với các tùy chọn Trung lập?

Ví dụ này minh họa về Cách tạo hộp thoại với các tùy chọn Trung lập.

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.

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

package com.example.andy.myapplication;
import android.content.DialogInterface;
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.View;
import android.widget.Toast;

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, R.style.CustomAlertDialog);
            builder.setTitle("Alert Dialog");
            builder.setMessage("Lorem Ipsum is simply dummy text of the printing and typesetting industry.
               Lorem Ipsum has been the industry's standard dummy text ever since the 1500s");
            builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Toast.makeText(MainActivity.this, "You have clicked yes", Toast.LENGTH_LONG).show();
               }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Toast.makeText(MainActivity.this, "You have clicked No", Toast.LENGTH_LONG).show();
               }
            });
            builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Toast.makeText(MainActivity.this, "You have clicked Neutral", Toast.LENGTH_LONG).show();
               }
            });
            final AlertDialog alertDialog = builder.create();
            alertDialog.show();
         }
      });
   }
}

Trong đoạn mã trên, khi người dùng nhấp vào nút, nó sẽ hiển thị hộp thoại có nút có, trung tính và không. Khi người dùng nhấp vào nút có, nó sẽ hiển thị thông báo có, không có nút nào hiển thị không có thông báo và nút trung lập cho thông báo trung lập.

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 một hộp thoại với các tùy chọn Trung lập?

Trong kết quả trên, nó hiển thị màn hình ban đầu. Bây giờ hãy nhấp vào nút, nó sẽ mở hộp thoại với các nút có, không và trung tính.

Làm thế nào để tạo một hộp thoại với các tùy chọn Trung lập?

Bây giờ hãy nhấp vào nút trung tính, nó sẽ cho kết quả như hình dưới đây -

Làm thế nào để tạo một hộp thoại với các tùy chọn Trung lập?