[Android] dialog box of AlertDialog

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/sandalphon4869/article/details/100130382


A, AlertDialog

1. The simplest AlertDialog

AlertDialog.Builder builder=new AlertDialog.Builder(MainAcitivity.this);
builder.show();

Use AlertDialog.Builder class to create an object, passed in the context parameter, and then like Toast the same show () display.

Effect: simple to become confused behavior. It makes the screen look black, just tap the changes back to the original color. This is the ultimate simple AleatDialog.
Here Insert Picture Description

2. Add point component

Here Insert Picture Description
A total of AlertDialog interface can be divided into four regions. These four areas are optional.

  • Icon area: setIcon ()

  • Title area: setTitle ()

  • Content Area: setMessage () or setItems () or setSingleChoiceItems () or setMultiChoiceItems () or setAdapter () or setView () one.
    Content area can only set a content that can only set a function.

  • Button area: setPositiveButton (), setNegativeButton () , setNeutralButton ().
    These three are OK, negative, neutral meaning, of course, no practical significance just how to use all right, just to better understand.

[Question: how to exit AlertDialog? ]
A: With or without buttons, except for points AlertDialog will quit.
When the button, the button will exit.
When the content area is setItems (), click on the entry will withdraw; when is setMessage (), click the Message will not quit.

(1) setMessage () simple text

Mainly to demonstrate setMessage (), buttons, titles, icons just by the way the presentation dispensable.
Here Insert Picture Description

AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.banana);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        Toast.makeText(MainActivity.this,"Positive",Toast.LENGTH_SHORT).show();
    }
});
builder.setNegativeButton("Negative", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        Toast.makeText(MainActivity.this,"Negative",Toast.LENGTH_SHORT).show();
    }
});
builder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        Toast.makeText(MainActivity.this,"Neutral",Toast.LENGTH_SHORT).show();
    }
});
builder.show();

Here Insert Picture Description

(2) setItems () simple list of strings

Here Insert Picture Description
Here Insert Picture Description

AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setIcon(R.drawable.banana);
final String fruits[]={"watermelon","apple"};
builder.setItems(fruits, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        Toast.makeText(MainActivity.this,fruits[i],Toast.LENGTH_SHORT).show();
    }
});
builder.show();

(3) setSingleChoiceItems () radio list item

Here Insert Picture Description
checkedItems which is initialized check, check the position of the index starts at 0.

AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setIcon(R.drawable.banana);
final String fruits[]={"watermelon","apple"};
builder.setSingleChoiceItems(fruits,1, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        Toast.makeText(MainActivity.this,fruits[i],Toast.LENGTH_SHORT).show();
    }
});

Here Insert Picture Description

(4) setMultiChoiceItems () multiple choice list item

Here Insert Picture Description
checkedItems Shi new boolean[]{}initialization which of several tick, true check, false is not checked.

Here Insert Picture Description


AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setIcon(R.drawable.banana);
final String fruits[]={"watermelon","apple"};
builder.setMultiChoiceItems(fruits, new boolean[]{true, false}, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
        Toast.makeText(MainActivity.this,fruits[i],Toast.LENGTH_SHORT).show();
    }
});
builder.show();

(5) setAdapter () custom list items

Here Insert Picture Description
【Android】AlertDialog与ListAdapter

(6) setView () Custom View

Two steps:

  • View to define the layout, add your control
  • Another example of the layout, setView () binding.

View defined layout: layout.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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello"
        android:id="@+id/fruit_name"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/banana"/>

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

绑定 setView (): MainActivity.java

AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Title");
LinearLayout linearLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.layout,null);
builder.setView(linearLayout);
builder.show();

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/sandalphon4869/article/details/100130382