ตัวอย่างนี้สาธิตเกี่ยวกับวิธีการสร้างกล่องโต้ตอบด้วยตัวเลือกที่เป็นกลาง
ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่
ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parent" xmlns:tools="http://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>
ในโค้ดด้านบนเราได้นำปุ่ม เมื่อผู้ใช้คลิกที่ปุ่ม มันจะแสดงกล่องโต้ตอบ
ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน 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();
}
});
}
} ในโค้ดด้านบน เมื่อผู้ใช้คลิกที่ปุ่ม มันจะแสดงกล่องโต้ตอบที่มีปุ่มใช่ เป็นกลาง และไม่ใช่ เมื่อผู้ใช้คลิกปุ่มใช่จะแสดงข้อความใช่ไม่มีปุ่มไม่แสดงข้อความและปุ่มกลางสำหรับข้อความที่เป็นกลาง
มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

ในผลลัพธ์ข้างต้น มันแสดงหน้าจอเริ่มต้น ตอนนี้คลิกที่ปุ่ม จะเปิดกล่องโต้ตอบที่มีปุ่มใช่ ไม่ใช่ และเป็นกลาง

ตอนนี้คลิกที่ปุ่มเป็นกลางก็จะให้ผลลัพธ์ดังที่แสดงด้านล่าง -
