Dialog eight basic dialog boxes

1. Simple Getting Started Dialog

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Normal dialog 1"
        android:onClick="onClick"
        />
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="normal dialog 2"
        android:onClick="onClick"
        />
</LinearLayout>

MainActivity.java

package com.administrator.dialogtest;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn1;
    private Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                showNormalDialog ();
                break;
            case R.id.btn2:
                showNormalDialog2 ();
        }
    }

    private void showNormalDialog2() {
        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setTitle("Prompt");//This is a method with no return value, so you can't keep .set like the first button
        dialog.setMessage("Please rate this class");
        //5 3 1
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "1分", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"You selected: 5 points",Toast.LENGTH_SHORT).show();
            }
        });
        dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "5 points", new DialogInterface.OnClickListener() {//This cannot pass null, because null does not know whether it is an empty Message object or an empty OnClickListener object
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "3分", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        //Be sure to call the show method, otherwise the dialog box will not be displayed
        dialog.show();
    }

    private void showNormalDialog() {
        //AlertDialog
        //AlerDialog's constructor is modified as protected
        //So it is not accessible outside the package, so we use the constructor
// AlertDialog dialog = new AlertDialog(this);//Because the method is defined as protected, use the constructor
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
// builder.setTitle("Title").setMessage("Hello").setPositiveButton()//Because the return value of each method is a builder, it can be written like this
        //set the title of the dialog
        builder.setTitle("提示");
        //set content
        builder.setMessage("Are you sure to exit the current program");
        //set button
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Exit the current program
                MainActivity.this.finish();
            }
        });
        builder.setNegativeButton("Cancel",null);//The click event can be passed like the OK button above, nothing is null
//        builder.setNeutralButton();
        //create dialog
//        builder.create();
        //Display method of dialog
        builder.show();//The create() method is written in the show() method, so there is no need to write it again
    }
}


Second, the list dialog box and the radio dialog box

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="list dialog"
        android:onClick="onClick"
        />
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="radio dialog"
        android:onClick="onClick"
        />
</LinearLayout>

MainActivity.java

package com.administrator.dialogtest;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn1;
    private Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                showListDialog ();
                break;
            case R.id.btn2:
                showSingleDialog();
        }
    }

    /**
     * radio dialog
     */
//    int idx = 0;
    private void showSingleDialog() {
        final String[] stars={"Lao Zhou","Lao Niu","Lao Sun","Lao","I can't write anymore"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("Please select your favorite star")
                //parameter 1: options
                //Parameter 2: default option, pass the index of an option
                //parameter 3: event when selected
                .setSingleChoiceItems(stars, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        idx = which;
                        Toast.makeText(MainActivity.this,"Your favorite star is:"+ stars[which],Toast.LENGTH_SHORT).show();
                    }
// }).setPositiveButton("OK",null);//If you need to click a button to disappear, set it like this
//                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
// public void onClick(DialogInterface dialog, int which) {//Which is not the same as the above, so to solve the problem passed down above, define a global variable
// Toast.makeText(MainActivity.this, "You selected:" + stars[idx], Toast.LENGTH_SHORT).show();
//                    }
//                });
                });
        builder.show();
    }

    /**
     * List dialog
     */
    private void showListDialog() {
        final String[] items = {"I am 1","I am 2","I am 3","I am 4"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("Please select:")
                //set list item
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {//Parameter 2: The index of the clicked item
                        Toast.makeText(MainActivity.this, "You selected:" + items[which], Toast.LENGTH_SHORT).show();
                    }
                });
        builder.show();
    }
}

3. Multiple selection dialog and waiting dialog

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Multiple selection dialog"
        android:onClick="onClick"
        />
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="wait for dialog"
        android:onClick="onClick"
        />
</LinearLayout>

MainActivity.java

package com.administrator.dialogtest;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn1;
    private Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                showMultiDialog();
                break;
            case R.id.btn2:
                showWaitingDialog();
        }
    }



    /**
     * Multiple selection dialog
     */
    private void showMultiDialog() {
        final String[] sports = {"basketball","tennis","table tennis","hockey","skateboard","swimming"};
        final boolean[] checked = {true,false,true,true,false,false};
        AlertDialog.Builder builder =new AlertDialog.Builder(this)
                .setTitle("your favorite sport")
                //parameter 1: options
                //Parameter 2: Default option (true: selected, false: not selected)
                //parameter 3: the event that is triggered when it is selected
                .setMultiChoiceItems(sports,checked , new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            //parameter 1: the dialog itself
                            //parameter 2: the index of the button
                            //Parameter 3: Whether the flag button is selected true (checked) false (unchecked)
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                // Whether it is selected or deselected, the onclick method will be triggered
                                //According to the which array subscript, save the selected state of isChecked to the array checked
// checked[which]=isChecked;//This method does this step by default. So no need to write
                                Log.e("Log", sports[which]);
                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String msg="Your hobby is: ";
                                for (int i = 0;i<checked.length;i++){
                                    if(checked[i]){
                                        msg += sports[i] + " ";//traverse the array of selected states just saved above
                                    }
                                }
                                Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
                            }
                });
//                });
        builder.show();
    }

    /**
     * wait for dialog
     */
    private void showWaitingDialog() {
        //Progress dialog, the default style is to circle
        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setTitle("I am a waiting dialog");
        dialog.setMessage("Please wait...");
// dialog.setCancelable(false);//Set whether to cancel, the default is true to cancel
// dialog.dismiss();//Set the dialog to disappear
        dialog.show();
    }
}

Fourth, the progress bar dialog box and input dialog box

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="progress bar dialog"
        android:onClick="onClick"
        />
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="input dialog"
        android:onClick="onClick"
        />
</LinearLayout>

MainActivity.java

package com.administrator.dialogtest;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn1;
    private Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                showProgressDialog();
                break;
            case R.id.btn2:
                showInputDialog();
        }
    }

    /**
     * Progress bar dialog
     */
    private void showProgressDialog() {
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setTitle("Downloading...");
        dialog.setMessage("Please wait");
        dialog.setIndeterminate(false);//Set the progress bar to be blurred, feel that the progress bar is moving, true is circular movement
        //Set the style of the dialog to horizontal style
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// dialog.setProgress(40);//Set progress
        new Thread(){
            public void run(){
                super.run();
                // Let the progress bar move from 1 to 100
                for(int i =1;i<=100;i++){
                    dialog.setProgress(i);
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace ();
                    }
                }
                dialog.dismiss();
            }
        }.start();
        dialog.show();
    }

    /**
     * input dialog
     */
    private void showInputDialog() {
        final EditText editText = new EditText(this);
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("Title")
                .setView(editText)//You need to pass a layout or a View view, or you can create a new layout in the resource folder and import it
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"What you entered is:"+editText.getText().toString(),Toast.LENGTH_SHORT).show();
                    }
                });
        builder.show();
    }
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325936280&siteId=291194637