瘋狂 Android 講義, 4/e – 對話盒(AlertDialog)複合範例『simple、simpleList、singleChoice、multiChoice、customList、customView』 P161~P167
瘋狂 Android 講義, 4/e – 對話盒(AlertDialog)複合範例『simple、simpleList、singleChoice、multiChoice、customList、customView』 P161~P167
資料來源:
https://github.com/daichangya/book/tree/master/android
https://pan.baidu.com/s/1d_xYJI0UQ_1tQzSj_V_NIg 提取码:70ch
https://suragch.medium.com/creating-a-custom-alertdialog-bae919d2efa5
範例01.XML Code – login
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/loginForm" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow android:paddingStart="12dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户名:" android:textSize="20sp" /> <!-- 输入用户名的文本框 --> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请填写登录账号" android:selectAllOnFocus="true" /> </TableRow> <TableRow android:paddingStart="12dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="密码:" android:textSize="20sp" /> <!-- 输入密码的文本框 --> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请填写密码" android:inputType="textPassword" /> </TableRow> <TableRow android:paddingStart="12dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="电话号码:" android:textSize="20sp" /> <!-- 输入电话号码的文本框 --> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请填写您的电话号码" android:inputType="phone" android:selectAllOnFocus="true" /> </TableRow> </TableLayout>
範例01.XML Code – array_item
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:shadowColor="#ff0" android:shadowDx="5" android:shadowDy="5" android:shadowRadius="2" android:textColor="#f0f" android:padding="8dp" android:textSize="20sp" />
範例01.XML Code – activity_main
<?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:gravity="center_horizontal" android:orientation="vertical"> <!-- 显示一个普通的文本编辑框组件 --> <EditText android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" /> <!-- 定义一个普通的按钮组件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="simple" android:text="简单对话框" /> <!-- 定义一个普通的按钮组件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="simpleList" android:text="简单列表项对话框" /> <!-- 定义一个普通的按钮组件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="singleChoice" android:text="单选列表项对话框" /> <!-- 定义一个普通的按钮组件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="multiChoice" android:text="多选列表项对话框" /> <!-- 定义一个普通的按钮组件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="customList" android:text="自定义列表项对话框" /> <!-- 定义一个普通的按钮组件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="customView" android:text="自定义View对话框" /> </LinearLayout>
範例01.Java Code – MainActivity
package org.crazyit.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TableLayout;
import android.widget.TextView;
/**
* Description:<br>
* 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a><br>
* Copyright (C), 2001-2020, Yeeku.H.Lee<br>
* This program is protected by copyright laws.<br>
* Program Name:<br>
* Date:<br>
*
* @author Yeeku.H.Lee kongyeeku@163.com<br>
* @version 1.0
*/
public class MainActivity extends Activity
{
private TextView show;
private String[] items = new String[]{"疯狂Java讲义", "疯狂前端开发讲义",
"轻量级Java EE企业应用实战", "疯狂Android讲义"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = findViewById(R.id.show);
}
public void simple(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 设置对话框标题
.setTitle("简单对话框")
// 设置图标
.setIcon(R.drawable.tools).setMessage("对话框的测试内容\n第二行内容");
// 为AlertDialog.Builder添加“确定”按钮
setPositiveButton(builder);
// 为AlertDialog.Builder添加“取消”按钮
setNegativeButton(builder).create().show();
}
private AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder)
{
// 调用setPositiveButton方法添加“确定”按钮
return builder.setPositiveButton("确定", (dialog, which) -> show.setText("单击了【确定】按钮!"));
}
private AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder)
{
// 调用setNegativeButton方法添加“取消”按钮
return builder.setNegativeButton("取消", (dialog, which) -> show.setText("单击了【取消】按钮!"));
}
public void simpleList(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 设置对话框标题
.setTitle("简单列表项对话框")
// 设置图标
.setIcon(R.drawable.tools)
// 设置简单的列表项内容
.setItems(items, (dialog, wh) -> show.setText("你选中了《" + items[wh] + "》"));
// 为AlertDialog.Builder添加“确定”按钮
setPositiveButton(builder);
// 为AlertDialog.Builder添加“取消”按钮
setNegativeButton(builder).create().show();
}
public void singleChoice(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 设置对话框标题
.setTitle("单选列表项对话框")
// 设置图标
.setIcon(R.drawable.tools)
// 设置单选列表项,默认选中第二项(索引为1)
.setSingleChoiceItems(items, 1, (dialog, which)
-> show.setText("你选中了《" + items[which] + "》"));
// 为AlertDialog.Builder添加“确定”按钮
setPositiveButton(builder);
// 为AlertDialog.Builder添加“取消”按钮
setNegativeButton(builder).create().show();
}
public void multiChoice(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 设置对话框标题
.setTitle("多选列表项对话框")
// 设置图标
.setIcon(R.drawable.tools)
// 设置多选列表项,设置勾选第2项、第4项
.setMultiChoiceItems(items, new boolean[]{false, true, false, true}, null);
// 为AlertDialog.Builder添加“确定”按钮
setPositiveButton(builder);
// 为AlertDialog.Builder添加“取消”按钮
setNegativeButton(builder).create().show();
}
public void customList(View source)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this)
// 设置对话框标题
.setTitle("自定义列表项对话框")
// 设置图标
.setIcon(R.drawable.tools)
// 设置自定义列表项
.setAdapter(new ArrayAdapter(this, R.layout.array_item, items), null);
// 为AlertDialog.Builder添加“确定”按钮
setPositiveButton(builder);
// 为AlertDialog.Builder添加“取消”按钮
setNegativeButton(builder).create().show();
}
public void customView(View source)
{
// 加载\res\layout\login.xml界面布局文件
TableLayout loginForm = (TableLayout) getLayoutInflater().inflate(R.layout.login, null);
new AlertDialog.Builder(this)
// 设置对话框的图标
.setIcon(R.drawable.tools)
// 设置对话框的标题
.setTitle("自定义View对话框")
// 设置对话框显示的View对象
.setView(loginForm)
// 为对话框设置一个“确定”按钮
.setPositiveButton("登录", (dialog, which) -> {
// 此处可执行登录处理
})
// 为对话框设置一个“取消”按钮
.setNegativeButton("取消", (dialog, which) -> {
// 取消登录,不做任何事情
})
// 创建并显示对话框
.create().show();
}
}
—————–
範例02.XML Code – custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
範例02.Java Code – MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showAlertDialogButtonClicked(View view) {
// create an alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Name");
// set the custom layout
final View customLayout = getLayoutInflater().inflate(R.layout.custom_layout, null);
builder.setView(customLayout);
// add a button
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// send data from the AlertDialog to the Activity
EditText editText = customLayout.findViewById(R.id.editText);
sendDialogDataToActivity(editText.getText().toString());
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
// do something with the data coming from the AlertDialog
private void sendDialogDataToActivity(String data) {
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
}
}