Article Directory
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.
2. Add point component
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.
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();
(2) setItems () simple list of strings
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
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();
}
});
(4) setMultiChoiceItems () multiple choice list item
checkedItems Shi new boolean[]{}
initialization which of several tick, true check, false is not checked.
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
【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();