当前位置:网站首页>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
边栏推荐
- 【微服务|SCG】gateway整合sentinel
- Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors
- ArcGIS: two methods of attribute fusion of the same field of vector elements
- GEE(三):计算两个波段间的相关系数与相应的p值
- Interview questions: how to test app performance?
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
- Byte hexadecimal binary understanding
- Brush question 5
- 聊聊 Dart 的空安全 (null safety) 特性
- USB(十六)2022-04-28
猜你喜欢
Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
leetcode-520. 检测大写字母-js
消息队列与快递柜之间妙不可言的关系
GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
The wonderful relationship between message queue and express cabinet
DTC社群运营怎么做?
聊聊支付流程的设计与实现逻辑
微信论坛交流小程序系统毕业设计毕设(7)中期检查报告
ArcGIS:矢量要素相同字段属性融合的两种方法
随机推荐
网络安全-对操作系统进行信息查询
Talk about DART's null safety feature
Brush question 5
I wish you all the best and the year of the tiger
About idea cannot find or load the main class
Circumvention Technology: Registry
网格(Grid)
微信论坛交流小程序系统毕业设计毕设(2)小程序功能
FPGA基础篇目录
[language programming] exe virus code example
Cases of agile innovation and transformation of consumer goods enterprises
Network security -burpsuit
Advantages and disadvantages of rest ful API
Handling file exceptions
Dynamics 365 查找字段过滤
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
Classification and prediction of heartbeat signal
网络安全-安装CentOS
肠道里的微生物和皮肤上的一样吗?
How to operate DTC community?