Android Dialog Control

dialog control


1. Overview
Dialog boxes are commonly used controls in UI design. In the Windows operating system, dialog boxes can be divided into modal dialog boxes and modeless dialog boxes .

 

When the modal dialog is in use, other UIs in the project are not allowed to operate, such as the save dialog box

Modeless dialogs allow other UI to be manipulated.

Android's dialog mode is a modeless dialog, but it can be set to a modal dialog.
Android provides rich dialog functions and allows custom dialogs. The following introduces the most widely used and feature-rich AlertDialog control.


The AlertDialog class provides four predefined dialog boxes:
1. Dialog boxes with messages and multiple buttons.
2. Dialogs with lists and buttons.
3. Dialog with radio list items and buttons.
4. Dialog with check list items and buttons.
5. Custom dialog box


2. Common methods

1、public void show()
Role: start and display the dialog box.
2、public void dismiss()
Action: Cancel the dialog.
3、public void setCancelable(boolean cancelable)
Role: Set whether to allow the return key to close the dialog box.
When the parameter -cancelable value is true, the return key is allowed to close the dialog box, false: not allowed

 


3. The inner class Bulider is the inner class of the
AlertDialog class, which is used to create a dialog box.

 

Properties commonly used in the AlertDialog.Builder class.

Properties commonly used in the AlertDialog.Builder class.
XML attribute description
icon dialog icon
title dialog title
message text displayed in the message dialog

 

(2) Common methods

1、Builder setPositionButton(CharSequence text,final

OnClickListener listener)

Role: Create a button with a certain meaning for the dialog box and return an object of the AlertDialog.Builder class.

Parameter -text: the title of the button
Parameter -listener: The implementation class object of the interface of the click event.

hint:
The package where the OnClickListener interface is located is: android.content.DialogInterface, which is different from the package of the previously learned interface of the same name. This interface is described in detail later.
The button created by setPositionButton() represents a button with definite meaning, such as the title is OK, OK, etc.

2、public Bulider setNegativeButton(CharSequence text,final

OnClickListener listener)

Role: Create a button with a negative, Cancel title, and return a

AlertDialog.Builder object.

Tip: Android's dialog object can create up to three buttons.

3、Builder setNeutralButton(CharSequence text,final

OnClickListener listener)
Role: Create a button with ignored meaning and return an AlertDialog.Builder object.

4、public Builder setIcon(int resId)
Function: Set the icon for the dialog, the parameter is the resource index value of the icon.

5、public Builder setTitle(int resId)
public Builder setTitle(CharSequence text)
Function: Set the dialog box title. The parameter can be a string or a string defined in the resource.

6、public Builder setMessage(int resId)
public Builder setMessage(CharSequence text)
Function: Set the information displayed in the dialog box. The parameter can be a string or a string defined in the resource.

7、public Builder setCancelable(boolean cancelable)
Effect: Sets the dialog box as a modal dialog box, and does not allow the Back key to close the dialog box.

8、public Builder setView(View view)
Role: Set the layout represented by the view as the content of the dialog.

9、AlertDialog create()
Role: Create and return a dialog object of type AlertDialog.




 

Specific examples:

package com.jxust.day05_07_alertdialogdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_main);
		setListener();
	}

	private void setListener() {
		setStandardDialogClickListener();
		setItemsDialogClickListener ();
		setCustomDialogClickListener();

	}

	// custom dialog listener
	private void setCustomDialogClickListener() {
		findViewById(R.id.btnCustomDialog).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// The role of View's inflate is to parse a view object into a java object
				// Parse cutom_dialog.xml into a View object
				View layout = View.inflate(MainActivity.this, R.layout.cutom_dialog, null);
				final EditText etAddress = (EditText) layout.findViewById(R.id.etAddress);
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("Dialog box for entering address")
					// Deploy the custom View object to the custom dialog to display the custom layout in the custom dialog
					.setView(layout)
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							String address = etAddress.getText().toString();
							Log.i("main", address);
						}
					});
				AlertDialog dialog = builder.create();
				dialog.show();
			}
		});
	}

	// listener for list dialog
	private void setItemsDialogClickListener() {
		findViewById(R.id.btnItemsDialog).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setItems(new String[]{"Add data","Delete data","Modify data"}, new DialogInterface.OnClickListener() {
					
					@Override
					// which represents the first element of items
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case 0:
							Toast.makeText(MainActivity.this, "Execute the operation of adding data", 3000).show();
							break;
						case 1:
							Toast.makeText(MainActivity.this, "Execute the operation to delete data", 3000).show();
							break;
						case 2:
							Toast.makeText(MainActivity.this, "Execute the operation to modify the data", 3000).show();
							break;
						}
					}
				});
				AlertDialog dialog = builder.create();
				dialog.show();
			}
		});
	}

	// listener for standard dialog
	private void setStandardDialogClickListener() {
		findViewById(R.id.btnStandardDialog).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//Create a Builder object for the dialog
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				// set the content of the dialog
				builder.setTitle("Exit the exam window")
					.setMessage("Exit exam?")
					// The role of DialogInterface is to distinguish it from the View.OnClickListener() above
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
								Log.i("main", "Exit exam");
						}
					})
					.setNegativeButton("放弃", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Log.i("main", "Continue exam");
						}
					});
				// create dialog
				AlertDialog dialog = builder.create();
				// show dialog
				dialog.show();
			}
		});
	}

}

 

Standard dialog



 

 

list dialog



 

custom dialog



 

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnStandardDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="standard dialog" />
    
    <Button
        android:id="@+id/btnItemsDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="list dialog" />
    
    <Button
        android:id="@+id/btnCustomDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="custom dialog" />
    

</LinearLayout>

 

The xml file required in the custom dialog

<?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="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="input address" />

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

</LinearLayout>

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326780139&siteId=291194637