“Dialog Peringatan Menu di Android” Kode Jawaban

Kotak dialog Alert Studio Android

package org.geeksforgeeks.navedmalik.alertdialog;
  
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
  
    // Declare the onBackPressed method
    // when the back button is pressed
    // this method will call
    @Override
    public void onBackPressed()
    {
  
        // Create the object of
        // AlertDialog Builder class
        AlertDialog.Builder builder
            = new AlertDialog
                  .Builder(MainActivity.this);
  
        // Set the message show for the Alert time
        builder.setMessage("Do you want to exit ?");
  
        // Set Alert Title
        builder.setTitle("Alert !");
  
        // Set Cancelable false
        // for when the user clicks on the outside
        // the Dialog Box then it will remain show
        builder.setCancelable(false);
  
        // Set the positive button with yes name
        // OnClickListener method is use of
        // DialogInterface interface.
  
        builder
            .setPositiveButton(
                "Yes",
                new DialogInterface
                    .OnClickListener() {
  
                        @Override
                        public void onClick(DialogInterface dialog,
                                            int which)
                        {
  
                            // When the user click yes button
                            // then app will close
                            finish();
                        }
                    });
  
        // Set the Negative button with No name
        // OnClickListener method is use
        // of DialogInterface interface.
        builder
            .setNegativeButton(
                "No",
                new DialogInterface
                    .OnClickListener() {
  
                        @Override
                        public void onClick(DialogInterface dialog,
                                            int which)
                        {
  
                            // If user click no
                            // then dialog box is canceled.
                            dialog.cancel();
                        }
                    });
  
        // Create the Alert dialog
        AlertDialog alertDialog = builder.create();
  
        // Show the Alert Dialog box
        alertDialog.show();
    }
}
Attractive Addax

Dialog Peringatan Menu di Android

//create an instance of AlertDialog.Builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);

//get string array (languages)
final String[] language = getResources().getStringArray(R.array.languages);

builder.setTitle(getText(R.string.select_language_title))

        //Set language(list of languages) to be displayed in the dialog as the content.
        .setItems(language, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int indexPosition) {

                // The 'indexPosition' argument contains the index position
                // of the selected item
                String selectedItem = Arrays.asList(language).get(indexPosition);
                Toast.makeText(getApplicationContext(), "Selected Language: " + selectedItem, Toast.LENGTH_LONG).show();

            }
        });

//create an AlertDialog
AlertDialog alertDialog = builder.create();

//display the dialog on screen
alertDialog.show();
Zany Zebra

Jawaban yang mirip dengan “Dialog Peringatan Menu di Android”

Pertanyaan yang mirip dengan “Dialog Peringatan Menu di Android”

Lebih banyak jawaban terkait untuk “Dialog Peringatan Menu di Android” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya