当前位置:网站首页>Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog

Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog

2022-07-07 23:14:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

AlertDialog Dialog box that can generate various contents . But every dialog box has such a structure :

Like the one below :

This is just the simplest dialog .

Let's look at the steps required to create a dialog :

1、 Use to create AlertDialog.Builder object

2、 call AlertDialog.Builder Of setTitle() or setCustomTitle() Method to set the title

3、 call AlertDialog.Builder Of setIcon() Method settings Icon

4、 Call some other setting methods to set the title

5、 call AlertDialog.Builder Of setPositiveButton()、setNegativeButton() perhaps setNeutralButton() Join multiple button

6、 call create() Method creation AlertDialog object , Call again AlertDialog Object's show() Method to display the dialog box .

newly build Android project , Then write main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"	android:orientation="vertical"	android:layout_width="match_parent"	android:layout_height="match_parent"	android:gravity="center_horizontal"><!--  Display a normal text edit box component  --><EditText     android:id="@+id/show"	android:layout_width="match_parent" 	android:layout_height="wrap_content" 	android:editable="false"/><!--  Define an ordinary button Components  --><Button	android:layout_width="match_parent" 	android:layout_height="wrap_content" 	android:text=" Simple dialog "	android:onClick="simple"	/><!--  Define an ordinary button Components  --><Button	android:layout_width="match_parent" 	android:layout_height="wrap_content" 	android:text=" Simple list item dialog "	android:onClick="simpleList"	/>	<!--  Define an ordinary button Components  --><Button	android:layout_width="match_parent" 	android:layout_height="wrap_content" 	android:text=" Radio list item dialog "	android:onClick="singleChoice"	/>	<!--  Define an ordinary button Components  --><Button	android:layout_width="match_parent" 	android:layout_height="wrap_content" 	android:text=" Multi select list item dialog box "	android:onClick="multiChoice"	/>	<!--  Define an ordinary button Components  --><Button	android:layout_width="match_parent" 	android:layout_height="wrap_content" 	android:text=" Define the list item dialog box by yourself "	android:onClick="customList"	/>	<!--  Define an ordinary button Components  --><Button	android:layout_width="match_parent" 	android:layout_height="wrap_content" 	android:text=" Define your own View Dialog box "	android:onClick="customView"	/>					</LinearLayout>

Here is the definition of 6 individual button And a text display box . And the corresponding onClick attribute

Next , We will write the main interface java Code :AlertDialogTest.java

package org.crazyit.ui;

import android.app.Activity;
import android.app.AlertDialog;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TableLayout;
import android.widget.TextView;


public class AlertDialogTest extends Activity
{
	TextView show;
	String[] items = new String[] { 
			" insane Java The notes ", " insane Ajax The notes ",
			" Lightweight Java EE Enterprise application practice ",
			" insane Android The notes " };	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		show = (TextView) findViewById(R.id.show);
	}

	public void simple(View source)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
			//  Set dialog title 
			.setTitle(" This is the dialog title ")
			//  Set icon 
			.setIcon(R.drawable.tools)
			.setMessage(" This is the content of the dialog ");
		//  by AlertDialog.Builder Join in 【 determine 】button
		setPositiveButton(builder);
		//  by AlertDialog.Builder Join in 【 Cancel 】button
		setNegativeButton(builder)
			.create()
			.show();
	}

	public void simpleList(View source)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
			//  Set dialog title 
			.setTitle(" Simple list item dialog ")
			//  Set icon 
			.setIcon(R.drawable.tools)
			//  Set simple list item content 
			.setItems(items, new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					show.setText(" You chose 《" + items[which] + "》");
				}
			});
		//  by AlertDialog.Builder Join in 【 determine 】button
		setPositiveButton(builder);
		//  by AlertDialog.Builder Join in 【 Cancel 】button
		setNegativeButton(builder)
			.create()
			.show();
	}

	public void singleChoice(View source)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
			//  Set dialog title 
			.setTitle(" Radio list item dialog ")
			//  Set icon 
			.setIcon(R.drawable.tools)
			//  Set radio list items , The second item is selected by default ( The index for 1)
			.setSingleChoiceItems(items, 1, new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					show.setText(" You chose 《" + items[which] + "》");
				}
			});
		//  by AlertDialog.Builder Join in 【 determine 】button
		setPositiveButton(builder);
		//  by AlertDialog.Builder Join in 【 Cancel 】button
		setNegativeButton(builder)
			.create()
			.show();
	}
	public void multiChoice(View source)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
			//  Set dialog title 
			.setTitle(" Multi select list item dialog box ")
			//  Set icon 
			.setIcon(R.drawable.tools)
			//  Set multiple list items , Set tick No 2 term 、 The first 4 term 
			.setMultiChoiceItems(items
			, new boolean[]{false , true ,false ,true}, null);
		//  by AlertDialog.Builder Join in 【 determine 】button
		setPositiveButton(builder);
		//  by AlertDialog.Builder Join in 【 Cancel 】button
		setNegativeButton(builder)
			.create()
			.show();
	}
	public void customList(View source)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
			//  Set dialog title 
			.setTitle(" Define the list item dialog box by yourself ")
			//  Set icon 
			.setIcon(R.drawable.tools)
			//  Set self defined list items 
			.setAdapter(new ArrayAdapter<String>(this 
					, R.layout.array_item 
					, items), null);
		//  by AlertDialog.Builder Join in 【 determine 】button
		setPositiveButton(builder);
		//  by AlertDialog.Builder Join in 【 Cancel 】button
		setNegativeButton(builder)
			.create()
			.show();
	}
	
	public void customView(View source)
	{
		// load /res/layout/login.xml Interface layout 
		TableLayout loginForm = (TableLayout)getLayoutInflater()
			.inflate( R.layout.login, null);		
		new AlertDialog.Builder(this)
			//  Set the icon of the dialog 
			.setIcon(R.drawable.tools)
			//  Set the title of the dialog 
			.setTitle(" Define your own View Dialog box ")
			//  Set the dialog box to display View object 
			.setView(loginForm)
			//  Set a... For the dialog “ determine ”button
			.setPositiveButton(" Sign in " , new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog,
						int which)
				{
					//  Login processing can be run here 
				}
			})
			//  Set a... For the dialog “ Cancel ”button
			.setNegativeButton(" Cancel ", new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog,
						int which)
				{
					//  Sign out . Don't do anything .				}			})			//  establish 、 And display the dialog box 			.create()			.show();	}		private AlertDialog.Builder setPositiveButton(			AlertDialog.Builder builder)	{		//  call setPositiveButton Method add ok button		return builder.setPositiveButton(" determine ", new OnClickListener()		{			@Override			public void onClick(DialogInterface dialog, int which)			{				show.setText(" Click 【 determine 】button!");			}		});	}		private AlertDialog.Builder setNegativeButton(			AlertDialog.Builder builder)	{		//  call setNegativeButton Method join cancel button		return builder.setNegativeButton(" Cancel ", new OnClickListener()		{			@Override			public void onClick(DialogInterface dialog, int which)			{				show.setText(" Click 【 Cancel 】button!");			}		});	}}

Right here. , The fifth and sixth button Two styles are used :array_item.xml and login.xml

Let's take a look at their content :

array_item.xml:

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/TextView"    android:textColor="#f0f"    android:textSize="30dp"    android:shadowColor="#ff0"    android:shadowRadius="2"    android:shadowDx="5"    android:shadowDy="5"	android:layout_width="match_parent" 	android:layout_height="wrap_content" />

login.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/loginForm"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
<TableRow>
<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="username:"
	android:textSize="10pt"
	/>
<!--  Input username Text box  -->
<EditText
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:hint=" Please fill in the login account "
	android:selectAllOnFocus="true"
	/>
</TableRow>
<TableRow>
<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="password:"
	android:textSize="10pt"	
	/>
<!--  Input password Text box  -->	
<EditText
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:hint=" Please fill in password"	
	android:password="true"
	/>
</TableRow>
<TableRow>
<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text=" Phone number :"
	android:textSize="10pt"	
	/>
<!--  Enter the text box for the phone number  -->		
<EditText
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:hint=" Please fill in your phone number "
	android:selectAllOnFocus="true"
	android:phoneNumber="true"
	/>
</TableRow>
</TableLayout>

adopt AlertDialog Be able to make dialog boxes of different styles , It is more practical in many times

And we can determine button To pass the data Intent Transfer to another interface .

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116202.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207072149334754.html