提交 68f36e08 编写于 作者: D dallascao

1

上级
---
name: Bug report
about: Something is crashing or not working as intended
labels: bug
---
如果你有能力,请考虑提出 pull 项要求。请注意,2.0.0 之前的版本不再受支持。
库版本:
2.X.X
受影响的设备:
搭载 Android9.0 的谷歌 Pixel3XL
描述这个错误:
对错误是什么有一个清晰的描述。
复制:
1.
2.
3.
预期行为:
清楚地描述你预期会发生的事情。
---
name: Feature request
about: Suggest an idea for this project
labels: improvement
---
如果你有能力,请考虑提出 pull 项要求。请注意,2.0.0 之前的版本不再受支持。
这适用于什么模块?
核心?输入?文件?颜色?
描述你希望发生的事情:
如果你希望实现的特性或行为,请提供清晰的描述。
描述一下你考虑过的替代方案:
清楚地描述你考虑过的任何替代解决方案。
---
name: Bug report
about: Something is crashing or not working as intended
labels: bug
---
*Please consider making a Pull Request if you are capable of doing so. Note that versions before 2.0.0 are no longer supported.*
**Library Version:**
2.x.x
**Affected Device(s):**
Google Pixel 3 XL with Android 9.0
**Describe the Bug:**
A clear description of what is the bug is.
**To Reproduce:**
1.
2.
3.
**Expected Behavior:**
A clear description of what you expected to happen.
---
name: Feature request
about: Suggest an idea for this project
labels: improvement
---
*Please consider making a Pull Request if you are capable of doing so. Note that versions before 2.0.0 are no longer supported.*
**What module does this apply to?**
Core? Input? Files? Color?
**Description what you'd like to happen:**
A clear description if the feature or behavior you'd like implemented.
**Describe alternatives you've considered:**
A clear description of any alternative solutions you've considered.
### Guidelines
1. You must run the `spotlessApply` task before commiting, either through Android Studio or with `./gradlew spotlessApply`.
2. A PR should be focused and contained. If you are changing multiple unrelated things, they should be in separate PRs.
3. A PR should fix a bug or solve a problem - something that only you would use is not necessarily something that should be published.
4. Give your PR a detailed title and description - look over your code one last time before actually creating the PR. Give it a self-review.
**If you do not follow the guidelines, your PR will be rejected.**
### 指导方针
1. 在提交之前,你必须运行 `spotlessApply` 任务,可以通过 Android Studio,也可以使用 `./gradlew spotlessApply`
2. 公关应该是集中和控制的。如果你正在更改多个不相关的事情,它们应该在单独的 PRS 中。
3. 公关人员应该修复一个 bug 或解决一个问题--只有你才会使用的东西不一定是应该发布的东西。
4. 给你的 PR 一个详细的标题和描述-在实际创建 PR 之前,最后一次查看你的代码。自我检讨一下。
如果你不遵守准则,你的公关将被拒绝。
此差异已折叠。
# 实质性对话框
#### [查看发布和更改日志](https://github.com/afollestad/material-dialogs/releases)
[![Android CI](https://github.com/afollestad/material-dialogs/workflows/Android%20CI/badge.svg)](https://github.com/afollestad/material-dialogs/actions?query=workflow%3A%22Android+CI%22)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/0a4acc30a9ce440087f7688735359bb8)](https://www.codacy.com/app/drummeraidan_50/material-dialogs?utm_source=github.com&utm_medium=referral&utm_content=afollestad/material-dialogs&utm_campaign=Badge_Grade)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
---
![Showcase](https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/showcase4.png)
# 模块
核心模块是使用此库所需的基本模块。其他是对 核心 的扩展。
请注意,由于材料对话 2。x.x,这个库只支持 Kotlin 。最新的 Java 版本是 `0.9.6.0`,可以找到 [here](README_OLD.md)。请注意,0.9.6.0 是不支持的,错误和改进将不会对该版本。
## Core
[ ![Core](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/core?label=core&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/core)
#### [核心教程和示例](documentation/CORE.md)
`core` 模块包含了启动库所需的所有内容。它包含所有核心和正常使用的功能。
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/basic_with_buttons.png" width="250px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:core:3.3.0'
}
```
## 输入
[ ![Input](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/input?label=input&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/input)
#### [输入教程和示例](documentation/INPUT.md)
`input` 模块包含对核心模块的扩展,例如文本输入对话框。
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/input.png" width="250px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:input:3.3.0'
}
```
## 档案
[ ![Files](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/files?label=files&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/files)
#### [文件教程和示例](documentation/FILES.md)
`files` 模块包含对核心模块的扩展,例如文件和文件夹选择器。
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_chooser.png" width="250px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:files:3.3.0'
}
```
## 颜色
[ ![Color](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/color?label=color&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/color)
#### [颜色教程和示例](documentation/COLOR.md)
`color` 模块包含对核心模块的扩展,例如颜色选择器。
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/color_chooser.png" width="250px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:color:3.3.0'
}
```
## 日期时间
[ ![DateTime](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/datetime?label=datetime&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/datetime)
#### [DateTime 教程和示例](documentation/DATETIME.md)
`datetime` 模块包含用于制作日期、时间和日期-时间选择器对话框的扩展。
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/datetimepicker.png" width="500px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:datetime:3.3.0'
}
```
## 底页
[ ![Bottom Sheets](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/bottomsheets?label=bottomsheets&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/bottomsheets)
#### [底页教程和示例](documentation/BOTTOMSHEETS.md)
`bottomsheets` 模块包含将模态对话框转换为底部工作表的扩展,以及显示项目网格等其他功能。一定要为此签出样例项目!
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/bottomsheet_customview.png" width="250px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:bottomsheets:3.3.0'
}
```
## 生命周期
[ ![Lifecycle](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/lifecycle?label=lifecycle&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/lifecycle)
#### [生命周期教程和示例](documentation/LIFECYCLE.md)
`lifecycle` 模块包含使对话框与 AndroidX 生命周期一起工作的扩展。
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:lifecycle:3.3.0'
}
```
此差异已折叠。
3.3.0
* 添加了 `md_line_spacing_body` 全局主题属性,该属性设置了消息行间距的全局默认值。见 #1903。
* 添加了一些断言和健全检查,以避免选择列表适配器出界崩溃。见 #1906。
* 底片对话框的底部不应使用角点半径。见 #1941。
* 修复用自定义字体切断的对话框标题。见 #1936。
* 如果 `noVerticalPadding` 设置为 `customView(...)``,则如果启用 `scrollable`,则不对内容 `ScrollView` 的底部应用填充。第 1834 号决议。
* 输入对话框样式不是由对话框强制执行的。使用 `TextInputLayout` 的全局默认值。见 #1857。
\ No newline at end of file
# 底页
## 目 录
1. [Gradle 依赖](#gradle-dependency)
2. [用法](#usage)
3. [布局模式](#layout-mode)
4. [窥视高度](#peek-height)
5. [项目网格](#item-grids)
6. [转角半径](#corner-radius)
---
## Gradle 依赖
[ ![Bottom Sheets](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/bottomsheets?label=bottomsheets&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/bottomsheets)
`bottomsheets` 模块包含将模态对话框转换为底部工作表的扩展,以及显示项目网格等其他功能。
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:bottomsheets:3.2.1'
}
```
## Usage
使对话框成为底部工作表非常简单,只需将 `BottomSheet` 的构造实例作为第二个参数传递给 `MaterialDialog` 的构造函数。
```kotlin
MaterialDialog(this, BottomSheet()).show {
...
}
```
---
## Layout Mode
有两种布局模式:
* `MATCH_PARENT`-默认值。底片可以展开以填充屏幕的高度。当打开时,底片将处于其窥视高度。
* `WRAP_CONTENT`-底页只能扩展到它所包含的视图的高度。如果它包含的视图与屏幕一样高或更高,则它仅限于屏幕高度,在这种情况下,内容也应该在 `ScrollView` 中。
布局模式是在 `BottomSheet` 行为的构造函数中设置的,如果你希望使用 `MATCH_PARENT`,则根本不需要显式地设置它:
```kotlin
MaterialDialog(this, BottomSheet(WRAP_CONTENT)).show {
...
}
```
---
## Peek Height
如果你以前使用过 Android 的底部工作表,窥探高度应该是一个熟悉的概念。窥视高度是指底板未完全展开时的高度。这是一个介于扩展和隐藏之间的点。
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/bottomsheet_peekheight.gif" width="250px" />
默认的窥视高度是屏幕高度的 60%。如果你愿意,可以设置自定义的窥视高度:
```kotlin
val dialog = MaterialDialog(this, BottomSheet()).show {
setPeekHeight(res = R.dimen.my_default_peek_height)
}
// You can continue to make calls to this method, and changes are still animated
dialog.setPeekHeight(res = R.dimen.another_peek_height)
```
窥视高度的变化是动画为你服务的。如果你使用 `WRAP_CONTENT` 布局模式,则窥视高度被限制为底页的最大高度。
---
## Item Grids
由于在底部工作表中显示项目的网格是常见的,因此此模块包含一个实现此目的的方法。
```kotlin
val items = listOf(
BasicGridItem(R.drawable.some_icon, "One"),
BasicGridItem(R.drawable.another_icon, "Two"),
BasicGridItem(R.drawable.hello_world, "Three"),
BasicGridItem(R.drawable.material_dialogs, "Four")
)
MaterialDialog(this, BottomSheet()).show {
...
gridItems(items) { _, index, item ->
toast("Selected item ${item.title} at index $index")
}
}
```
请注意,如果你希望传递自定义项类型,`gridItems` 可以获取从 `GridItem` 接口继承的任何内容的列表。你只需要覆盖 `title` 值以及 `populateIcon(ImageView)`函数。
---
你可以提供一些额外的参数,其中大多数参数与你可以提供给 `listItems` 的参数相同。`customGridWidth` 是一个可选的整数资源,它允许你为网格设置宽度-你可以为不同的资源配置(平板电脑、景观等)设置不同的宽度。
```kotlin
fun gridItems(
items: List<IT : GridItem>,
@IntegerRes customGridWidth: Int? = null,
disabledIndices: IntArray? = null,
waitForPositiveButton: Boolean = true,
selection: GridItemListener<IT> = null
): MaterialDialog
```
---
### Corner Radius
这是取自核心模块文档。我在这里重申这一点是为了确保人们知道这是可能的,因为与常规的模态对话框相比,在底部工作表中使用舍入更常见。
角点半径可以通过应用程序主题中的一个属性进行全局更改。它默认为 4DP:
```xml
<style name="AppTheme.Custom" parent="Theme.AppCompat">
...
<item name="md_corner_radius">16dp</item>
</style>
```
上述功能会影响应用程序中的所有对话框,甚至是正常的模态对话框。对于该值还有一个编程设置器,你可以在每个对话框中使用它:
```kotlin
MaterialDialog(this, BottomSheet()).show {
// literal, internally converts to dp so 16dp
cornerRadius(16f)
// Using a dimen instead is encouraged as it's easier to have all instances changeable from one place
cornerRadius(res = R.dimen.my_corner_radius)
}
```
# 颜色
# 目录-颜色
1. [Gradle 依赖](#gradle-dependency)
2. [颜色选择器](#color-choosers)1。[基础知识](#basics)2。[副颜色](#sub-colors)
## Gradle 依赖
[ ![Color](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/color?label=color&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/color)
`color` 模块包含对核心模块的扩展,例如颜色选择器。
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:color:3.2.1'
}
```
## Color Choosers
### Basics
颜色选择器显示简单的颜色网格。
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/color_chooser.png" width="250px" />
```kotlin
val colors = intArrayOf(RED, GREEN, BLUE)
MaterialDialog(this).show {
title(R.string.colors)
colorChooser(colors) { dialog, color ->
// Use color integer
}
positiveButton(R.string.select)
}
```
你可以指定一个初始选择,它只是一个颜色整数:
```kotlin
val colors = intArrayOf(RED, GREEN, BLUE)
MaterialDialog(this).show {
title(R.string.colors)
colorChooser(colors, initialSelection = BLUE) { dialog, color ->
// Use color integer
}
positiveButton(R.string.select)
}
```
### Sub Colors
你可以指定子颜色,这些子颜色是从每个顶层颜色向下的一个级别。顶层数组的大小必须与子颜色数组的大小匹配。
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/color_chooser_sub.png" width="250px" />
```kotlin
val colors = intArrayOf(RED, GREEN, BLUE) // size = 3
val subColors = listOf( // size = 3
intArrayOf(LIGHT_RED, RED, DARK_RED, WHITE),
intArrayOf(LIGHT_GREEN, GREEN, DARK_GREEN, GRAY),
intArrayOf(LIGHT_BLUE, BLUE, DARK_BLUE, BLACK)
)
MaterialDialog(this).show {
title(R.string.colors)
colorChooser(colors, subColors = subColors) { dialog, color ->
// Use color integer
}
positiveButton(R.string.select)
}
```
### ARGB 选择
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/custom_argb.png" width="250px" />
```kotlin
MaterialDialog(this).show {
title(R.string.colors)
colorChooser(
colors = colors,
subColors = subColors,
allowCustomArgb = true,
showAlphaSelector = true
) { dialog, color ->
// Use color integer
}
positiveButton(R.string.select)
}
```
省略 `showAlphaSelector` 将隐藏 alpha(透明)选择器。
# 核心
## 目 录
1. [Gradle 依赖](#gradle-dependency)
2. [基础知识](#basics)
3. [自定义消息](#customizing-the-message)
4. [动作按钮](#action-buttons)
5. [添加图标](#adding-an-icon)
6. [回调](#callbacks)
7. [Dismissing](#dismissing)
8. [列表](#lists)1。[平原](#plain)2。[单一选择](#single-choice)3。[多项选择](#multiple-choice)4。[Custom Adapters](#custom-adapters)
9. [复选框提示](#checkbox-prompts)
10. [自定义视图](#custom-views)
11. [杂项](#miscellaneous)
12. [主题化](#theming)1。[光明与黑暗](#light-and-dark)2。[背景颜色](#background-color)3。[波纹颜色](#ripple-color)4。[转角半径](#corner-radius)5。[文字颜色](#text-color)6。[字体](#fonts)7。[Widget Color](#widget-color)
## Gradle 依赖
[ ![Core](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/core?label=core&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/core)
`core` 模块包含了启动库所需的所有内容。它包含所有核心和正常使用的功能。
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:core:3.2.1'
}
```
## Basics
下面是一个创建和显示对话框的基本示例:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/basic.png" width="250px" />
```kotlin
MaterialDialog(this).show {
title(R.string.your_title)
message(R.string.your_message)
}
```
`this` 应该是一个 `Context`,它连接到一个窗口,就像 `Activity`
如果你想要传入文字字符串而不是字符串资源:
```kotlin
MaterialDialog(this).show {
title(text = "Your Title")
message(text = "Your Message")
}
```
请注意,你也可以在不立即显示对话框的情况下设置对话框:
```kotlin
val dialog = MaterialDialog(this)
.title(R.string.your_title)
.message(R.string.your_message)
dialog.show()
```
## 自定义消息
`message`函数允许你使用 lambda 跟踪它,该 lambda 公开了某些内置的修饰符,并允许你直接对 `TextView` 进行操作。
```kotlin
MaterialDialog(this).show {
...
message(R.string.your_message) {
html() // format, color, etc. with tags in string
html { link -> // same as above, but...
// Invokes a callback when a URL is clicked instead of auto opening it in a browser
}
lineSpacing(1.4f) // modifies line spacing, default is 1.0f
// You can directly act on the message TextView as well
val textView = messageTextView
}
}
```
## Action Buttons
添加动作按钮的方法很简单:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/basic_with_buttons.png" width="250px" />
```kotlin
MaterialDialog(this).show {
positiveButton(R.string.agree)
negativeButton(R.string.disagree)
}
```
你也可以在这里使用文字字符串:
```kotlin
MaterialDialog(this).show {
positiveButton(text = "Agree")
negativeButton(text = "Disagree")
}
```
---
监听按钮的点击就像在末尾添加一个 lambda 一样简单:
```kotlin
MaterialDialog(this).show {
positiveButton(R.string.agree) { dialog ->
// Do something
}
negativeButton(R.string.disagree) { dialog ->
// Do something
}
}
```
如果动作按钮太长,不能与对话框的宽度相适应,它们将自动堆叠:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/stacked_buttons.png" width="250px" />
## Adding an Icon
你可以在标题的左侧显示一个图标:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/icon.png" width="250px" />
```kotlin
MaterialDialog(this).show {
icon(R.drawable.your_icon)
}
```
你还可以传递一个可拉伸实例:
```kotlin
val myDrawable: Drawable = // ...
MaterialDialog(this).show {
icon(drawable = myDrawable)
}
```
## Callbacks
有几个生命周期回调可以连接到:
```kotlin
MaterialDialog(this).show {
onPreShow { dialog -> }
onShow { dialog -> }
onDismiss { dialog -> }
onCancel { dialog -> }
}
```
## 解雇
取消对话框会关闭它,它只是从父类 `Dialog` 类继承的一个简单方法:
```kotlin
val dialog: MaterialDialog = // ...
dialog.dismiss()
```
---
你可以防止对话框被取消,这意味着必须通过操作按钮或调用上面的方法来明确地取消对话框。
```kotlin
MaterialDialog(this).show {
cancelable(false) // calls setCancelable on the underlying dialog
cancelOnTouchOutside(false) // calls setCanceledOnTouchOutside on the underlying dialog
}
```
## Lists
### Plain
你可以在 `MaterialDialog` 上使用 `listItems` 扩展来显示列表:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/basic_list.png" width="250px" />
```kotlin
MaterialDialog(this).show {
listItems(R.array.socialNetworks)
}
```
你也可以传递一个文字字符串数组:
```kotlin
val myItems = listOf("Hello", "World")
MaterialDialog(this).show {
listItems(items = myItems)
}
```
要获得项目选择事件,只需追加一个 lambda:
```kotlin
MaterialDialog(this).show {
listItems(R.array.socialNetworks) { dialog, index, text ->
// Invoked when the user taps an item
}
}
```
### Single Choice
你可以在 `MaterialDialog` 上使用 `listItemsSingleChoice` 扩展来显示单选(单选按钮)列表:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/single_choice_list.png" width="250px" />
```kotlin
MaterialDialog(this).show {
listItemsSingleChoice(R.array.my_items)
}
```
你也可以传递一个文字字符串数组:
```kotlin
val myItems = listOf("Hello", "World")
MaterialDialog(this).show {
listItemsSingleChoice(items = myItems)
}
```
---
如果希望在对话框打开时选择一个选项,可以传递 `initialSelection` 索引):
```kotlin
MaterialDialog(this).show {
listItemsSingleChoice(R.array.my_items, initialSelection = 1)
}
```
要获得项目选择事件,只需追加一个 lambda:
```kotlin
MaterialDialog(this).show {
listItemsSingleChoice(R.array.my_items) { dialog, index, text ->
// Invoked when the user selects an item
}
}
```
在没有操作按钮的情况下,当用户点击一个项目时,会立即调用选择回调。如果你添加一个积极的行动按钮...
```kotlin
MaterialDialog(this).show {
listItemsSingleChoice(R.array.my_items) { dialog, index, text ->
// Invoked when the user selects an item
}
positiveButton(R.string.select)
}
```
...然后,直到用户选择一个项目并点击“积极行动”按钮,才会调用回调。你可以使用 `waitForPositiveButton` 参数覆盖该行为。
额外的好处是,你可以禁用被选中 / 未选中的项目:
```kotlin
val indices = intArrayOf(0, 2)
MaterialDialog(this).show {
listItemsSingleChoice(R.array.my_items, disabledIndices = indices)
}
```
---
在构建的对话框中,你可以使用一些方法来修改选中的状态:
```kotlin
val dialog: MaterialDialog = // ...
dialog.checkItem(index)
dialog.uncheckItem(index)
dialog.toggleItemChecked(index)
val checked: Boolean = dialog.isItemChecked(index)
```
### Multiple Choice
你可以在 `MaterialDialog` 上使用 `listItemsMultiChoice` 扩展来显示多项选择(复选框)列表:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/multi_choice_list.png" width="250px" />
```kotlin
MaterialDialog(this).show {
listItemsMultiChoice(R.array.my_items) { _, index, text ->
// Invoked when the user selects item(s)
}
}
```
你也可以传递一个文字字符串数组:
```kotlin
val myItems = listOf("Hello", "World")
MaterialDialog(this).show {
listItemsMultiChoice(items = myItems)
}
```
---
如果希望在对话框打开时选择选项,可以传递 `initialSelection` 索引):
```kotlin
val indices = intArrayOf(1, 3)
MaterialDialog(this).show {
listItemsMultiChoice(R.array.my_items, initialSelection = indices)
}
```
要获得项目选择事件,只需追加一个 lambda:
```kotlin
MaterialDialog(this).show {
listItemsMultiChoice(R.array.my_items) { dialog, indices, items ->
// Invoked when the user selects an item
}
}
```
在没有操作按钮的情况下,当用户点击一个项目时,会立即调用选择回调。如果你添加一个积极的行动按钮...
```kotlin
MaterialDialog(this).show {
listItemsMultiChoice(R.array.my_items) { dialog, indices, items ->
// Invoked when the user selects an item
}
positiveButton(R.string.select)
}
```
...然后,直到用户选择一个或多个项目并点击“积极行动”按钮,才会调用回调。你可以使用 `waitForPositiveButton` 参数覆盖该行为。
额外的好处是,你可以禁用被选中 / 未选中的项目:
```kotlin
val indices = intArrayOf(0, 2)
MaterialDialog(this).show {
listItemsMultiChoice(R.array.my_items, disabledIndices = indices)
}
```
---
在构建的对话框中,你可以使用一些方法来修改选中的状态:
```kotlin
val dialog: MaterialDialog = // ...
val indices: IntArray = // ...
dialog.checkItems(indices)
dialog.uncheckItems(indices)
dialog.toggleItemsChecked(indices)
dialog.checkAllItems()
dialog.uncheckAllItems()
dialog.toggleAllItemsChecked()
val checked: Boolean = dialog.isItemChecked(index)
```
### 自定义适配器
如果希望自定义列表以使用自己的视图,则需要使用自定义适配器。
```kotlin
val adapter: RecyclerView.Adapter<*> = // some sort of adapter implementation...
MaterialDialog(this).show {
customListAdapter(adapter)
}
```
稍后可以从对话框实例中再次检索适配器:
```kotlin
val dialog: MaterialDialog = // ...
val adapter: RecyclerView.Adapter<*> = dialog.getListAdapter()
```
你还可以检索适配器托管在以下位置的 `RecyclerView`:
```kotlin
val dialog: MaterialDialog = // ...
val recyclerView: RecyclerView = dialog.getRecyclerView()
```
## 复选框提示
复选框提示可以与任何其他对话框类型一起使用,它将在显示操作按钮的同一视图中显示。
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/checkbox_prompt.png" width="250px" />
```kotlin
MaterialDialog(this).show {
checkBoxPrompt(R.string.your_label) { checked ->
// Check box was checked or unchecked
}
}
```
你也可以为标签传递一个文字字符串:
```kotlin
MaterialDialog(this).show {
checkBoxPrompt(text = "Hello, World")
}
```
---
你还可以追加一个 lambda,该 lambda 在选中或未选中复选框时被调用:
```kotlin
MaterialDialog(this).show {
checkBoxPrompt(text = "Hello, World") { checked -> }
}
```
如果你只关心按下“积极行动”按钮时的复选框状态:
```kotlin
MaterialDialog(this).show {
checkBoxPrompt(R.string.your_label)
positiveButton(R.string.button_text) { dialog ->
val isChecked = dialog.isCheckPromptChecked()
// do something
}
}
```
## Custom Views
许多包含的扩展都使用自定义视图,例如颜色选择器对话框。在示例项目中还有一个简单的示例。
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/custom_view.png" width="250px" />
```kotlin
MaterialDialog(this).show {
customView(R.layout.my_custom_view)
}
```
你还可以传递一个字面视图:
```kotlin
val myView: View = // ...
MaterialDialog(this).show {
customView(view = myView)
}
```
如果你的自定义视图可能比对话框更高,那么你将希望使其成为可滚动的:
```kotlin
MaterialDialog(this).show {
customView(R.layout.my_custom_view, scrollable = true)
}
```
对于以后的访问,你可以使用 `dialog.getCustomView()`:
```kotlin
val dialog = MaterialDialog(this)
.customView(R.layout.my_custom_view, scrollable = true)
val customView = dialog.getCustomView()
// Use the view instance, e.g. to set values or setup listeners
dialog.show()
```
## Miscellaneous
有一些细节很容易漏掉。例如,自动解除控制,无论按下操作按钮或点击列表项是否会自动解除对话框。默认情况下,它是打开的。你可以禁用它:
```kotlin
MaterialDialog(this).show {
noAutoDismiss()
}
```
## Theming
谷歌新的主题思维方式(相对于 2014 年的思维方式)是灵活的。如果你拿他们的
["Crane example"](https://material.io/design/components/dialogs.html#theming),你看到他们
更改字体、转角舍入等。
### Light and Dark
根据应用程序的主题,自动设置明暗主题(基本上是 `android:textColorPrimary` 是更亮还是更暗):
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/lightanddarkthemes.png" width="500px" />
### 背景颜色
Material Dialogs 在活动主题中使用 `colorBackgroundFloating` 属性的值作为 Dialogs 的背景色。你还可以在主题中使用 `md_background_color` 属性,该属性将优先使用。
### Ripple Color
Material Dialogs 默认情况下使用活动主题中的 `?android:colorControlHighlight` 属性的值作为列表项、按钮等的波纹颜色。你也可以使用 `md_ripple_color` 主题属性重写此内容。
### Corner Radius
角点半径是对话框角的舍入:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/customtheme.png" width="250px" />
它可以通过应用程序主题中的一个属性进行更改。它默认为 4DP:
```xml
<style name="AppTheme.Custom" parent="Theme.AppCompat">
<item name="md_corner_radius">16dp</item>
</style>
```
这个值也有一个编程设置器:
```kotlin
MaterialDialog(this).show {
// literal, internally converts to dp so 16dp
cornerRadius(16f)
// Using a dimen instead is encouraged as it's easier to have all instances changeable from one place
cornerRadius(res = R.dimen.my_corner_radius)
}
```
### Text Color
默认情况下,来自 ActivityTheme 的 `android:textColorPrimary``android:textColorSecondary` 属性用于对话框的标题和内容颜色。`colorPrimary` 用于动作按钮的默认文本颜色。如果你希望重写这些内容,可以提供以下属性:
```xml
<style name="AppTheme.Custom" parent="Theme.AppCompat">
<item name="md_color_title">@color/your_color</item>
<item name="md_color_content">@color/your_color</item>
<item name="md_color_button_text">@color/your/color</item>
</style>
```
### Fonts
这个库支持使用自定义字体,由支持库 `ResourcesCompat` 类提供支持。使用 `/res/font` 文件夹中的原始字体文件或 XML 字体文件,你可以使用应用程序主题中的属性在实质性对话框中使用它们。
```xml
<style name="AppTheme.Custom" parent="Theme.AppCompat">
<item name="md_font_title">@font/your_font</item>
<item name="md_font_body">@font/your_font</item>
<item name="md_font_button">@font/your_font</item>
</style>
```
请参阅示例项目中的“自定义主题”示例(打开主题切换器的溢出菜单)。
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/customtheme.png" width="250px" />
# 日期时间
## 目 录
1. [Gradle 依赖](#gradle-dependency-4)
2. [日期](#date)
3. [时间](#time)
4. [日期时间](#datetime)
## Gradle 依赖
[ ![DateTime](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/datetime?label=datetime&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/datetime)
`datetime` 模块包含用于制作日期、时间和日期-时间选择器对话框的扩展。
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:datetime:3.2.1'
}
```
## Date
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/datepicker.png" width="250px" />
```kotlin
MaterialDialog(this).show {
...
datePicker { dialog, date ->
// Use date (Calendar)
}
}
```
还可以选择提供 `minDate``maxDate``currentDate` 参数。
## Time
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/timepicker.png" width="250px" />
```kotlin
MaterialDialog(this).show {
...
timePicker { dialog, time ->
// Use time (Calendar)
}
}
```
你还可以选择提供 `currentTime``show24HoursView` 参数。
## DateTime
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/datetimepicker.png" width="400px" />
```kotlin
MaterialDialog(this).show {
...
dateTimePicker(requireFutureDateTime = true) { _, dateTime ->
// Use dateTime (Calendar)
}
}
```
还可以选择提供 `minDateTime``maxDate``currentDateTime``show24HoursView``requireFutureDateTime` 参数。
# 档案
## 目 录
1. [Gradle 依赖](#gradle-dependency)
2. [文件选择器](#file-choosers)1。[基础知识](#basics)2。[过滤器](#filter)3。[空文本](#empty-text)4。[文件夹创建](#folder-creation)
3. [文件夹选择器](#folder-choosers)1。[基础知识](#basics-1)2。[过滤器](#filter-1)3。[空文本](#empty-text-1)4。[文件夹创建](#folder-creation-1)
## Gradle 依赖
[ ![Files](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/files?label=files&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/files)
`files` 模块包含对核心模块的扩展,例如文件和文件夹选择器。
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:files:3.2.1'
}
```
## File Choosers
### Basics
注意:文件选择器要求你的应用程序具有 `READ_EXTERNAL_STORAGE` 的权限,否则目录列表将返回为空。
`MaterialDialog` 上使用 `fileChooser` 扩展名创建文件选择器:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_chooser.png" width="250px" />
```kotlin
MaterialDialog(this).show {
fileChooser { dialog, file ->
// File selected
}
}
```
它显示了所有的文件和文件夹,从外部存储目录开始。点击文件会调用回调并取消对话框。
你可以更改最初列出的目录:
```kotlin
val initialFolder = File(getExternalStorageDirectory(), "Download")
MaterialDialog(this).show {
fileChooser(initialDirectory = initialFolder) { dialog, file ->
// File selected
}
}
```
如果存在一个积极的行动按钮,点击一个文件将选择它,但回调是不是调用,直到积极的行动按钮被按下。
### Filter
可以应用一个过滤器来只显示你希望显示的文件和目录:
```kotlin
// show ALL folders, and files that start with the letter 'a'
val myFilter: FileFilter = { it.isDirectory || it.nameWithoutExtension.startsWith("a", true) }
MaterialDialog(this).show {
fileChooser(filter = myFilter) { dialog, file ->
// File selected
}
}
```
### Empty Text
当文件夹中没有内容时,将显示空文本。你可以配置空文本标签:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_emptytext.png" width="250px" />
```kotlin
MaterialDialog(this).show {
fileChooser(emptyTextRes = R.string.custom_label) { dialog, file ->
// File selected
}
}
```
### Folder Creation
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_folder_creation.png" width="250px" />
你可以允许用户创建文件夹。
```kotlin
MaterialDialog(this).show {
fileChooser(
allowFolderCreation = true,
folderCreationLabel = R.string.new_folder // optional as well
) { dialog, file ->
// File selected
}
}
```
此“新建文件夹”选项仅在可写的目录中显示。
## Folder Choosers
注意:文件夹选择器要求你的应用程序具有 `READ_EXTERNAL_STORAGE` 的权限,否则目录列表将返回为空。
文件夹选择器与文件选择器基本相同,但有几个小区别:1)即使应用了自定义过滤器,也只显示文件夹。2)选择回调不会在项目单击时调用,只有在按下“积极行动”按钮时,才会在当前查看的文件夹中调用它。
### Basics
```kotlin
MaterialDialog(this).show {
folderChooser { dialog, folder ->
// Folder selected
}
}
```
### Filter
你可以像使用文件选择器一样应用过滤器。
```kotlin
// show only folders that start with the letter 'a'
val myFilter: FileFilter = { it.name.startsWith("a", true) }
MaterialDialog(this).show {
folderChooser(filter = myFilter) { dialog, file ->
// Folder selected
}
}
```
### Empty Text
当文件夹中没有内容时,将显示空文本。你可以配置空文本标签:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_emptytext.png" width="250px" />
```kotlin
MaterialDialog(this).show {
folderChooser(emptyTextRes = R.string.custom_label) { dialog, file ->
// File selected
}
}
```
### Folder Creation
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_folder_creation.png" width="250px" />
你可以允许用户创建文件夹。
```kotlin
MaterialDialog(this).show {
folderChooser(
allowFolderCreation = true,
folderCreationLabel = R.string.new_folder // optional as well
) { dialog, file ->
// File selected
}
}
```
此“新建文件夹”选项仅在可写的目录中显示。
# 输入
## 目 录
1. [Gradle 依赖](#gradle-dependency)
2. [文本输入](#text-input)1。[基础知识](#basics)2。[提示和预置](#hints-and-prefill)3。[输入类型](#input-types)4。[最大长度](#max-length)5。[自定义验证](#custom-validation)
## Gradle 依赖
[ ![Input](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/input?label=input&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/input)
`input` 模块包含对核心模块的扩展,例如文本输入对话框。
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:input:3.3.0'
}
```
## Text Input
### Basics
你可以在 `MaterialDialog` 上使用 `input` 扩展来设置输入对话框:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/input.png" width="250px" />
```kotlin
MaterialDialog(this).show {
input()
positiveButton(R.string.submit)
}
```
通过设置输入对话框,你可以检索输入字段:
```kotlin
val dialog: MaterialDialog = // ...
val inputField: EditText = dialog.getInputField()
```
---
当按下输入文本的“积极行动”按钮时,可以追加 lambda 以接收回调:
```kotlin
MaterialDialog(this).show {
input { dialog, text ->
// Text submitted with the action button
}
positiveButton(R.string.submit)
}
```
如果将 `waitForPositiveButton` 设置为 false,则每次修改文本字段时都会调用回调:
```kotlin
MaterialDialog(this).show {
input(waitForPositiveButton = false) { dialog, text ->
// Text changed
}
positiveButton(R.string.done)
}
```
即使输入为空,也可以按下“积极行动”按钮:
```kotlin
MaterialDialog(this).show {
input(allowEmpty = true) { dialog, text ->
// Text submitted with the action button, might be an empty string`
}
positiveButton(R.string.done)
}
```
### 提示和预置
你可以为输入字段设置一个提示,这是当字段为空时显示的灰色褪色文本:
```kotlin
MaterialDialog(this).show {
input(hintRes = R.string.hint_text)
}
```
也可以使用文字字符串:
```kotlin
MaterialDialog(this).show {
input(hint = "Your Hint Text")
}
```
---
你还可以对输入字段进行预填充:
```kotlin
MaterialDialog(this).show {
input(prefillRes = R.string.prefill_text)
}
```
也可以使用文字字符串:
```kotlin
MaterialDialog(this).show {
input(prefill = "Prefilled text")
}
```
### Input Types
你可以将输入类型应用到输入字段,当字段被关注时,输入字段会修改键盘类型。这是从 Android framework 开始的,输入类型被直接应用到底层 `EditText`:
```kotlin
val type = InputType.TYPE_CLASS_TEXT or
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
MaterialDialog(this).show {
input(inputType = type)
}
```
### Max Length
你可以设置一个最大长度,使字符计数器可见,如果输入长度超过该长度,则禁用“积极行动”按钮:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/input_max_length.png" width="250px" />
```kotlin
MaterialDialog(this).show {
input(maxLength = 8)
positiveButton(R.string.submit)
}
```
### 自定义验证
你可以使用 Input Listener 进行自定义验证。本例强制输入以字母“A”开头:
```kotlin
MaterialDialog(this).show {
input(waitForPositiveButton = false) { dialog, text ->
val inputField = dialog.getInputField()
val isValid = text.startsWith("a", true)
inputField?.error = if (isValid) null else "Must start with an 'a'!"
dialog.setActionButtonEnabled(POSITIVE, isValid)
}
positiveButton(R.string.submit)
}
```
# 生命周期
## 目 录
1. [Gradle 依赖](#gradle-dependency)
2. [用法](#usage)
## Gradle 依赖
[ ![Lifecycle](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/lifecycle?label=lifecycle&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/lifecycle)
`lifecycle` 模块包含一些扩展,可以使对话框与 AndroidX 生命周期一起工作。
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:lifecycle:3.2.1'
}
```
## Usage
```kotlin
MaterialDialog(this).show {
...
lifecycleOwner(owner)
}
```
当给定的生命周期所有者被销毁时,对话框将自动被解散。生命周期所有者包括来自 AndroidX 的活动和片段,以及实现 `LifecycleOwner` 接口的任何类。
# Bottom Sheets
## Table of Contents
1. [Gradle Dependency](#gradle-dependency)
2. [Usage](#usage)
3. [Layout Mode](#layout-mode)
4. [Peek Height](#peek-height)
5. [Item Grids](#item-grids)
6. [Corner Radius](#corner-radius)
---
## Gradle Dependency
[ ![Bottom Sheets](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/bottomsheets?label=bottomsheets&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/bottomsheets)
The `bottomsheets` module contains extensions to turn modal dialogs into bottom sheets, among
other functionality like showing a grid of items.
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:bottomsheets:3.2.1'
}
```
## Usage
Making a dialog a bottom sheet is as simple as passing a constructed instance of `BottomSheet`
as the second parameter to `MaterialDialog`'s constructor.
```kotlin
MaterialDialog(this, BottomSheet()).show {
...
}
```
---
## Layout Mode
There are two layout modes:
* `MATCH_PARENT` - the default. The bottom sheet can be expanded to fill the height of the screen.
When opened, the bottom sheet will be at its peek height.
* `WRAP_CONTENT` - the bottom sheet can only be expanded as far as the height of the view it contains.
If the view it contains is as tall or taller than the screen, it's limited to the screen height, in
which case the content should be in a `ScrollView` as well.
The layout mode is set in the constructor of the `BottomSheet` behavior, you don't need to
explicitly set it at all if you wish to use `MATCH_PARENT`:
```kotlin
MaterialDialog(this, BottomSheet(WRAP_CONTENT)).show {
...
}
```
---
## Peek Height
If you've used Android bottom sheets before, peek height should be a familiar concept. The peek
height is the height of the bottom sheet when it's not fully expanded. It's a point between
expanded and hidden.
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/bottomsheet_peekheight.gif" width="250px" />
The default peek height is 60% of the screen height. You can set a custom peek height if you wish:
```kotlin
val dialog = MaterialDialog(this, BottomSheet()).show {
setPeekHeight(res = R.dimen.my_default_peek_height)
}
// You can continue to make calls to this method, and changes are still animated
dialog.setPeekHeight(res = R.dimen.another_peek_height)
```
Changes to the peek height are animated for you. If you're using the `WRAP_CONTENT` layout mode,
the peek height is limited to the max height of your bottom sheet.
---
## Item Grids
Since it's common to show a grid of items in a bottom sheet, this module contains a method to do
that.
```kotlin
val items = listOf(
BasicGridItem(R.drawable.some_icon, "One"),
BasicGridItem(R.drawable.another_icon, "Two"),
BasicGridItem(R.drawable.hello_world, "Three"),
BasicGridItem(R.drawable.material_dialogs, "Four")
)
MaterialDialog(this, BottomSheet()).show {
...
gridItems(items) { _, index, item ->
toast("Selected item ${item.title} at index $index")
}
}
```
Note that `gridItems` can take a list of anything that inherits from the `GridItem` interface,
if you wish to pass a custom item type. You just need to override the `title` value along with the
`populateIcon(ImageView)` function.
---
There a few extra parameters that you can provide, most of which are equivelent to what you can
provide to `listItems`. `customGridWidth` is an optional integer resource that allows you to set a
width for the grid - you can have different widths for different resource configurations (tablet,
landscape, etc.)
```kotlin
fun gridItems(
items: List<IT : GridItem>,
@IntegerRes customGridWidth: Int? = null,
disabledIndices: IntArray? = null,
waitForPositiveButton: Boolean = true,
selection: GridItemListener<IT> = null
): MaterialDialog
```
---
### Corner Radius
This is taken from the core module documentation. I'm reiterating it here to make sure people
know that it is possible, since it's more common to use rounding with bottom sheets than regular
modal dialogs.
Corner radius can be globally changed with an attribute in your app theme. It defaults to 4dp:
```xml
<style name="AppTheme.Custom" parent="Theme.AppCompat">
...
<item name="md_corner_radius">16dp</item>
</style>
```
The above effects _all_ dialogs in your app, even the normal modal ones. There is also a
programmatic setter for this value which you can use per-dialog:
```kotlin
MaterialDialog(this, BottomSheet()).show {
// literal, internally converts to dp so 16dp
cornerRadius(16f)
// Using a dimen instead is encouraged as it's easier to have all instances changeable from one place
cornerRadius(res = R.dimen.my_corner_radius)
}
```
# Color
# Table of Contents - Color
1. [Gradle Dependency](#gradle-dependency)
2. [Color Choosers](#color-choosers)
1. [Basics](#basics)
2. [Sub Colors](#sub-colors)
## Gradle Dependency
[ ![Color](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/color?label=color&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/color)
The `color` module contains extensions to the core module, such as a color chooser.
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:color:3.2.1'
}
```
## Color Choosers
### Basics
Color choosers show a simple grid of colors.
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/color_chooser.png" width="250px" />
```kotlin
val colors = intArrayOf(RED, GREEN, BLUE)
MaterialDialog(this).show {
title(R.string.colors)
colorChooser(colors) { dialog, color ->
// Use color integer
}
positiveButton(R.string.select)
}
```
You can specify an initial selection, which is just a color integer:
```kotlin
val colors = intArrayOf(RED, GREEN, BLUE)
MaterialDialog(this).show {
title(R.string.colors)
colorChooser(colors, initialSelection = BLUE) { dialog, color ->
// Use color integer
}
positiveButton(R.string.select)
}
```
### Sub Colors
You can specify sub-colors, which are a level down from each top level color. The size of the top
level array must match the size of the sub-colors array.
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/color_chooser_sub.png" width="250px" />
```kotlin
val colors = intArrayOf(RED, GREEN, BLUE) // size = 3
val subColors = listOf( // size = 3
intArrayOf(LIGHT_RED, RED, DARK_RED, WHITE),
intArrayOf(LIGHT_GREEN, GREEN, DARK_GREEN, GRAY),
intArrayOf(LIGHT_BLUE, BLUE, DARK_BLUE, BLACK)
)
MaterialDialog(this).show {
title(R.string.colors)
colorChooser(colors, subColors = subColors) { dialog, color ->
// Use color integer
}
positiveButton(R.string.select)
}
```
### ARGB Selection
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/custom_argb.png" width="250px" />
```kotlin
MaterialDialog(this).show {
title(R.string.colors)
colorChooser(
colors = colors,
subColors = subColors,
allowCustomArgb = true,
showAlphaSelector = true
) { dialog, color ->
// Use color integer
}
positiveButton(R.string.select)
}
```
Omitting `showAlphaSelector` will hide the alpha (transparency) selector.
# Core
## Table of Contents
1. [Gradle Dependency](#gradle-dependency)
2. [Basics](#basics)
3. [Customizing the Message](#customizing-the-message)
4. [Action Buttons](#action-buttons)
5. [Adding an Icon](#adding-an-icon)
6. [Callbacks](#callbacks)
7. [Dismissing](#dismissing)
8. [Lists](#lists)
1. [Plain](#plain)
2. [Single Choice](#single-choice)
3. [Multiple Choice](#multiple-choice)
4. [Custom Adapters](#custom-adapters)
9. [Checkbox Prompts](#checkbox-prompts)
10. [Custom Views](#custom-views)
11. [Miscellaneous](#miscellaneous)
12. [Theming](#theming)
1. [Light and Dark](#light-and-dark)
2. [Background Color](#background-color)
3. [Ripple Color](#ripple-color)
4. [Corner Radius](#corner-radius)
5. [Text Color](#text-color)
6. [Fonts](#fonts)
7. [Widget Color](#widget-color)
## Gradle Dependency
[ ![Core](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/core?label=core&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/core)
The `core` module contains everything you need to get started with the library. It contains all
core and normal-use functionality.
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:core:3.2.1'
}
```
## Basics
Here's a very basic example of creating and showing a dialog:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/basic.png" width="250px" />
```kotlin
MaterialDialog(this).show {
title(R.string.your_title)
message(R.string.your_message)
}
```
`this` should be a `Context` which is attached to a window, like an `Activity`.
If you wanted to pass in literal strings instead of string resources:
```kotlin
MaterialDialog(this).show {
title(text = "Your Title")
message(text = "Your Message")
}
```
Note that you can setup a dialog without immediately showing it, as well:
```kotlin
val dialog = MaterialDialog(this)
.title(R.string.your_title)
.message(R.string.your_message)
dialog.show()
```
## Customizing the Message
The `message` function lets you trail it with a lambda, which exposes certain built-in modifiers
along with allowing you to act on the `TextView` directly.
```kotlin
MaterialDialog(this).show {
...
message(R.string.your_message) {
html() // format, color, etc. with tags in string
html { link -> // same as above, but...
// Invokes a callback when a URL is clicked instead of auto opening it in a browser
}
lineSpacing(1.4f) // modifies line spacing, default is 1.0f
// You can directly act on the message TextView as well
val textView = messageTextView
}
}
```
## Action Buttons
There are simple methods for adding action buttons:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/basic_with_buttons.png" width="250px" />
```kotlin
MaterialDialog(this).show {
positiveButton(R.string.agree)
negativeButton(R.string.disagree)
}
```
You can use literal strings here as well:
```kotlin
MaterialDialog(this).show {
positiveButton(text = "Agree")
negativeButton(text = "Disagree")
}
```
---
Listening for clicks on the buttons is as simple as adding a lambda to the end:
```kotlin
MaterialDialog(this).show {
positiveButton(R.string.agree) { dialog ->
// Do something
}
negativeButton(R.string.disagree) { dialog ->
// Do something
}
}
```
If action buttons together are too long to fit in the dialog's width, they will be automatically
stacked:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/stacked_buttons.png" width="250px" />
## Adding an Icon
You can display an icon to the left of the title:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/icon.png" width="250px" />
```kotlin
MaterialDialog(this).show {
icon(R.drawable.your_icon)
}
```
You can pass a Drawable instance as well:
```kotlin
val myDrawable: Drawable = // ...
MaterialDialog(this).show {
icon(drawable = myDrawable)
}
```
## Callbacks
There are a few lifecycle callbacks you can hook into:
```kotlin
MaterialDialog(this).show {
onPreShow { dialog -> }
onShow { dialog -> }
onDismiss { dialog -> }
onCancel { dialog -> }
}
```
## Dismissing
Dismissing a dialog closes it, it's just a simple method inherited from the parent `Dialog` class:
```kotlin
val dialog: MaterialDialog = // ...
dialog.dismiss()
```
---
You can prevent a dialog from being canceled, meaning it has to be explictly dismissed with an
action button or a call to the method above.
```kotlin
MaterialDialog(this).show {
cancelable(false) // calls setCancelable on the underlying dialog
cancelOnTouchOutside(false) // calls setCanceledOnTouchOutside on the underlying dialog
}
```
## Lists
### Plain
You can show lists using the `listItems` extension on `MaterialDialog`:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/basic_list.png" width="250px" />
```kotlin
MaterialDialog(this).show {
listItems(R.array.socialNetworks)
}
```
You can pass a literal string array too:
```kotlin
val myItems = listOf("Hello", "World")
MaterialDialog(this).show {
listItems(items = myItems)
}
```
To get item selection events, just append a lambda:
```kotlin
MaterialDialog(this).show {
listItems(R.array.socialNetworks) { dialog, index, text ->
// Invoked when the user taps an item
}
}
```
### Single Choice
You can show single choice (radio button) lists using the `listItemsSingleChoice` extension
on `MaterialDialog`:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/single_choice_list.png" width="250px" />
```kotlin
MaterialDialog(this).show {
listItemsSingleChoice(R.array.my_items)
}
```
You can pass a literal string array too:
```kotlin
val myItems = listOf("Hello", "World")
MaterialDialog(this).show {
listItemsSingleChoice(items = myItems)
}
```
---
If you want an option to be selected when the dialog opens, you can pass an `initialSelection` index):
```kotlin
MaterialDialog(this).show {
listItemsSingleChoice(R.array.my_items, initialSelection = 1)
}
```
To get item selection events, just append a lambda:
```kotlin
MaterialDialog(this).show {
listItemsSingleChoice(R.array.my_items) { dialog, index, text ->
// Invoked when the user selects an item
}
}
```
Without action buttons, the selection callback is invoked immediately when the user taps an item. If
you add a positive action button...
```kotlin
MaterialDialog(this).show {
listItemsSingleChoice(R.array.my_items) { dialog, index, text ->
// Invoked when the user selects an item
}
positiveButton(R.string.select)
}
```
...then the callback isn't invoked until the user selects an item *and* taps the positive action
button. You can override that behavior using the `waitForPositiveButton` argument.
An added bonus, you can disable items from being selected/unselected:
```kotlin
val indices = intArrayOf(0, 2)
MaterialDialog(this).show {
listItemsSingleChoice(R.array.my_items, disabledIndices = indices)
}
```
---
There are methods you can use in a built dialog to modify checked states:
```kotlin
val dialog: MaterialDialog = // ...
dialog.checkItem(index)
dialog.uncheckItem(index)
dialog.toggleItemChecked(index)
val checked: Boolean = dialog.isItemChecked(index)
```
### Multiple Choice
You can show multiple choice (checkbox) lists using the `listItemsMultiChoice` extension on `MaterialDialog`:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/multi_choice_list.png" width="250px" />
```kotlin
MaterialDialog(this).show {
listItemsMultiChoice(R.array.my_items) { _, index, text ->
// Invoked when the user selects item(s)
}
}
```
You can pass a literal string array too:
```kotlin
val myItems = listOf("Hello", "World")
MaterialDialog(this).show {
listItemsMultiChoice(items = myItems)
}
```
---
If you want option(s) to be selected when the dialog opens, you can pass an `initialSelection` index):
```kotlin
val indices = intArrayOf(1, 3)
MaterialDialog(this).show {
listItemsMultiChoice(R.array.my_items, initialSelection = indices)
}
```
To get item selection events, just append a lambda:
```kotlin
MaterialDialog(this).show {
listItemsMultiChoice(R.array.my_items) { dialog, indices, items ->
// Invoked when the user selects an item
}
}
```
Without action buttons, the selection callback is invoked immediately when the user taps an item. If
you add a positive action button...
```kotlin
MaterialDialog(this).show {
listItemsMultiChoice(R.array.my_items) { dialog, indices, items ->
// Invoked when the user selects an item
}
positiveButton(R.string.select)
}
```
...then the callback isn't invoked until the user select one or more items *and* taps the positive
action button. You can override that behavior using the `waitForPositiveButton` argument.
An added bonus, you can disable items from being selected/unselected:
```kotlin
val indices = intArrayOf(0, 2)
MaterialDialog(this).show {
listItemsMultiChoice(R.array.my_items, disabledIndices = indices)
}
```
---
There are methods you can use in a built dialog to modify checked states:
```kotlin
val dialog: MaterialDialog = // ...
val indices: IntArray = // ...
dialog.checkItems(indices)
dialog.uncheckItems(indices)
dialog.toggleItemsChecked(indices)
dialog.checkAllItems()
dialog.uncheckAllItems()
dialog.toggleAllItemsChecked()
val checked: Boolean = dialog.isItemChecked(index)
```
### Custom Adapters
If you want to customize lists to use your own views, you need to use a custom adapter.
```kotlin
val adapter: RecyclerView.Adapter<*> = // some sort of adapter implementation...
MaterialDialog(this).show {
customListAdapter(adapter)
}
```
You can retrieve your adapter again later from the dialog instance:
```kotlin
val dialog: MaterialDialog = // ...
val adapter: RecyclerView.Adapter<*> = dialog.getListAdapter()
```
You can also retrieve the `RecyclerView` that the adapter is hosted in:
```kotlin
val dialog: MaterialDialog = // ...
val recyclerView: RecyclerView = dialog.getRecyclerView()
```
## Checkbox Prompts
Checkbox prompts can be used together with any other dialog type, it gets shown in the same view
which shows the action buttons.
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/checkbox_prompt.png" width="250px" />
```kotlin
MaterialDialog(this).show {
checkBoxPrompt(R.string.your_label) { checked ->
// Check box was checked or unchecked
}
}
```
You can pass a literal string for the label too:
```kotlin
MaterialDialog(this).show {
checkBoxPrompt(text = "Hello, World")
}
```
---
You can also append a lambda which gets invoked when the checkbox is checked or unchecked:
```kotlin
MaterialDialog(this).show {
checkBoxPrompt(text = "Hello, World") { checked -> }
}
```
If you only care about the checkbox state when the positive action button is pressed:
```kotlin
MaterialDialog(this).show {
checkBoxPrompt(R.string.your_label)
positiveButton(R.string.button_text) { dialog ->
val isChecked = dialog.isCheckPromptChecked()
// do something
}
}
```
## Custom Views
A lot of the included extensions use custom views, such as the color chooser dialog. There's also
a simple example in the sample project.
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/custom_view.png" width="250px" />
```kotlin
MaterialDialog(this).show {
customView(R.layout.my_custom_view)
}
```
You can also pass a literal view:
```kotlin
val myView: View = // ...
MaterialDialog(this).show {
customView(view = myView)
}
```
If your custom view may be taller than the dialog, you'll want to make it scrollable:
```kotlin
MaterialDialog(this).show {
customView(R.layout.my_custom_view, scrollable = true)
}
```
For later access, you can use `dialog.getCustomView()`:
```kotlin
val dialog = MaterialDialog(this)
.customView(R.layout.my_custom_view, scrollable = true)
val customView = dialog.getCustomView()
// Use the view instance, e.g. to set values or setup listeners
dialog.show()
```
## Miscellaneous
There are little details which are easy to miss. For an example, auto dismiss controls whether pressing
the action buttons or tapping a list item will automatically dismiss the dialog or not. By default,
it's turned on. You can disable it:
```kotlin
MaterialDialog(this).show {
noAutoDismiss()
}
```
## Theming
Google's newer mindset with Material Theming (vs the 2014 mindset) is flexible. If you take their
["Crane example"](https://material.io/design/components/dialogs.html#theming), you see that they
change fonts, corner rounding, etc.
### Light and Dark
Light and dark theming is automatic based on your app's theme (basically whether `android:textColorPrimary`
is more light or more dark):
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/lightanddarkthemes.png" width="500px" />
### Background Color
Material Dialogs uses the value of the `colorBackgroundFloating` attribute in your Activity theme
for the background color of dialogs. You can also use the `md_background_color` attribute in your
theme, which will take precedence.
### Ripple Color
Material Dialogs uses the value of the `?android:colorControlHighlight` attribute in your Activity
theme for the ripple color of list items, buttons, etc. by default. You can override this with the
`md_ripple_color` theme attribute as well.
### Corner Radius
Corner radius is the rounding of dialog corners:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/customtheme.png" width="250px" />
it can be changed with an attribute in your app theme. It defaults to 4dp:
```xml
<style name="AppTheme.Custom" parent="Theme.AppCompat">
<item name="md_corner_radius">16dp</item>
</style>
```
There is also a programmatic setter for this value:
```kotlin
MaterialDialog(this).show {
// literal, internally converts to dp so 16dp
cornerRadius(16f)
// Using a dimen instead is encouraged as it's easier to have all instances changeable from one place
cornerRadius(res = R.dimen.my_corner_radius)
}
```
### Text Color
By default, `android:textColorPrimary` and `android:textColorSecondary` attributes from your Activity
theme are used for the title and content colors of dialogs. `colorPrimary` is used for the default
text color of action buttons. If you wish to override these, there are attributes provided:
```xml
<style name="AppTheme.Custom" parent="Theme.AppCompat">
<item name="md_color_title">@color/your_color</item>
<item name="md_color_content">@color/your_color</item>
<item name="md_color_button_text">@color/your/color</item>
</style>
```
### Fonts
This library supports using custom fonts, powered by the Support libraries `ResourcesCompat` class.
With raw font files or XML font files in your `/res/font` folder, you can use them in Material Dialogs
using attributes in your app's theme.
```xml
<style name="AppTheme.Custom" parent="Theme.AppCompat">
<item name="md_font_title">@font/your_font</item>
<item name="md_font_body">@font/your_font</item>
<item name="md_font_button">@font/your_font</item>
</style>
```
See the "Custom Theme" example in the sample project (open the overflow menu for the theme switcher).
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/customtheme.png" width="250px" />
# DateTime
## Table of Contents
1. [Gradle Dependency](#gradle-dependency-4)
2. [Date](#date)
3. [Time](#time)
4. [DateTime](#datetime)
## Gradle Dependency
[ ![DateTime](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/datetime?label=datetime&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/datetime)
The `datetime` module contains extensions to make date, time, and date-time picker dialogs.
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:datetime:3.2.1'
}
```
## Date
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/datepicker.png" width="250px" />
```kotlin
MaterialDialog(this).show {
...
datePicker { dialog, date ->
// Use date (Calendar)
}
}
```
You can optionally provide `minDate`, `maxDate` and `currentDate` parameters as well.
## Time
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/timepicker.png" width="250px" />
```kotlin
MaterialDialog(this).show {
...
timePicker { dialog, time ->
// Use time (Calendar)
}
}
```
You can optionally provide `currentTime` and `show24HoursView` parameters as well.
## DateTime
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/datetimepicker.png" width="400px" />
```kotlin
MaterialDialog(this).show {
...
dateTimePicker(requireFutureDateTime = true) { _, dateTime ->
// Use dateTime (Calendar)
}
}
```
You can optionally provide `minDateTime`, `maxDate`, `currentDateTime`, `show24HoursView`,
and `requireFutureDateTime` parameters as well.
# Files
## Table of Contents
1. [Gradle Dependency](#gradle-dependency)
2. [File Choosers](#file-choosers)
1. [Basics](#basics)
2. [Filter](#filter)
3. [Empty Text](#empty-text)
4. [Folder Creation](#folder-creation)
3. [Folder Choosers](#folder-choosers)
1. [Basics](#basics-1)
2. [Filter](#filter-1)
3. [Empty Text](#empty-text-1)
4. [Folder Creation](#folder-creation-1)
## Gradle Dependency
[ ![Files](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/files?label=files&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/files)
The `files` module contains extensions to the core module, such as a file and folder chooser.
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:files:3.2.1'
}
```
## File Choosers
### Basics
**Note:** File choosers require your app to have permission to `READ_EXTERNAL_STORAGE`, otherwise
directory listings will come back empty.
You create file choosers using the `fileChooser` extension on `MaterialDialog`:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_chooser.png" width="250px" />
```kotlin
MaterialDialog(this).show {
fileChooser { dialog, file ->
// File selected
}
}
```
It shows all files and folders, starting in the external storage directory. Tapping a file invokes
the callback and dismisses the dialog.
You can change the directory which is listed initially:
```kotlin
val initialFolder = File(getExternalStorageDirectory(), "Download")
MaterialDialog(this).show {
fileChooser(initialDirectory = initialFolder) { dialog, file ->
// File selected
}
}
```
**If a positive action button exists, tapping a file will select it, but the callback isn't invoked
until the positive action button is pressed.**
### Filter
A filter can be applied to only show the files and directories you wish to show:
```kotlin
// show ALL folders, and files that start with the letter 'a'
val myFilter: FileFilter = { it.isDirectory || it.nameWithoutExtension.startsWith("a", true) }
MaterialDialog(this).show {
fileChooser(filter = myFilter) { dialog, file ->
// File selected
}
}
```
### Empty Text
Empty text is shown when a folder has no contents. You can configure the empty text label:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_emptytext.png" width="250px" />
```kotlin
MaterialDialog(this).show {
fileChooser(emptyTextRes = R.string.custom_label) { dialog, file ->
// File selected
}
}
```
### Folder Creation
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_folder_creation.png" width="250px" />
You can allow your users to create folders.
```kotlin
MaterialDialog(this).show {
fileChooser(
allowFolderCreation = true,
folderCreationLabel = R.string.new_folder // optional as well
) { dialog, file ->
// File selected
}
}
```
This "New Folder" option is only show in directories which are writable.
## Folder Choosers
**Note:** Folder choosers require your app to have permission to `READ_EXTERNAL_STORAGE`, otherwise
directory listings will come back empty.
Folder choosers are basically the same as file choosers, with a few minor differences: 1) only folders
are shown, even when a custom filter is applied. 2) the selection callback is never invoked on a
item click, it only gets invoked with the currently viewed folder when the positive action button
is pressed.
### Basics
```kotlin
MaterialDialog(this).show {
folderChooser { dialog, folder ->
// Folder selected
}
}
```
### Filter
You can apply a filter like you can with the file chooser.
```kotlin
// show only folders that start with the letter 'a'
val myFilter: FileFilter = { it.name.startsWith("a", true) }
MaterialDialog(this).show {
folderChooser(filter = myFilter) { dialog, file ->
// Folder selected
}
}
```
### Empty Text
Empty text is shown when a folder has no contents. You can configure the empty text label:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_emptytext.png" width="250px" />
```kotlin
MaterialDialog(this).show {
folderChooser(emptyTextRes = R.string.custom_label) { dialog, file ->
// File selected
}
}
```
### Folder Creation
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_folder_creation.png" width="250px" />
You can allow your users to create folders.
```kotlin
MaterialDialog(this).show {
folderChooser(
allowFolderCreation = true,
folderCreationLabel = R.string.new_folder // optional as well
) { dialog, file ->
// File selected
}
}
```
This "New Folder" option is only show in directories which are writable.
# Input
## Table of Contents
1. [Gradle Dependency](#gradle-dependency)
2. [Text Input](#text-input)
1. [Basics](#basics)
2. [Hints and Prefill](#hints-and-prefill)
3. [Input Types](#input-types)
4. [Max Length](#max-length)
5. [Custom Validation](#custom-validation)
## Gradle Dependency
[ ![Input](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/input?label=input&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/input)
The `input` module contains extensions to the core module, such as a text input dialog.
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:input:3.3.0'
}
```
## Text Input
### Basics
You can setup an input dialog using the `input` extension on `MaterialDialog`:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/input.png" width="250px" />
```kotlin
MaterialDialog(this).show {
input()
positiveButton(R.string.submit)
}
```
With a setup input dialog, you can retrieve the input field:
```kotlin
val dialog: MaterialDialog = // ...
val inputField: EditText = dialog.getInputField()
```
---
You can append a lambda to receive a callback when the positive action button is pressed with
text entered:
```kotlin
MaterialDialog(this).show {
input { dialog, text ->
// Text submitted with the action button
}
positiveButton(R.string.submit)
}
```
If you set `waitForPositiveButton` to false, the callback is invoked every time the text field is
modified:
```kotlin
MaterialDialog(this).show {
input(waitForPositiveButton = false) { dialog, text ->
// Text changed
}
positiveButton(R.string.done)
}
```
To allow the positive action button to be pressed even when the input is empty:
```kotlin
MaterialDialog(this).show {
input(allowEmpty = true) { dialog, text ->
// Text submitted with the action button, might be an empty string`
}
positiveButton(R.string.done)
}
```
### Hints and Prefill
You can set a hint to the input field, which is the gray faded text shown when the field is empty:
```kotlin
MaterialDialog(this).show {
input(hintRes = R.string.hint_text)
}
```
A literal string can be used as well:
```kotlin
MaterialDialog(this).show {
input(hint = "Your Hint Text")
}
```
---
You can also prefill the input field:
```kotlin
MaterialDialog(this).show {
input(prefillRes = R.string.prefill_text)
}
```
A literal string can be used as well:
```kotlin
MaterialDialog(this).show {
input(prefill = "Prefilled text")
}
```
### Input Types
You can apply input types to the input field, which modifies the keyboard type when the field is
focused on. This is just taken right from the Android framework, the input type gets applied
directly to the underlying `EditText`:
```kotlin
val type = InputType.TYPE_CLASS_TEXT or
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
MaterialDialog(this).show {
input(inputType = type)
}
```
### Max Length
You can set a max length which makes a character counter visible, and disables the positive action
button if the input length goes over that:
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/input_max_length.png" width="250px" />
```kotlin
MaterialDialog(this).show {
input(maxLength = 8)
positiveButton(R.string.submit)
}
```
### Custom Validation
You can do custom validation using the input listener. This example enforces that the input
starts with the letter 'a':
```kotlin
MaterialDialog(this).show {
input(waitForPositiveButton = false) { dialog, text ->
val inputField = dialog.getInputField()
val isValid = text.startsWith("a", true)
inputField?.error = if (isValid) null else "Must start with an 'a'!"
dialog.setActionButtonEnabled(POSITIVE, isValid)
}
positiveButton(R.string.submit)
}
```
# Lifecycle
## Table of Contents
1. [Gradle Dependency](#gradle-dependency)
2. [Usage](#usage)
## Gradle Dependency
[ ![Lifecycle](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/lifecycle?label=lifecycle&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/lifecycle)
The `lifecycle` module contains extensions to make dialogs work with AndroidX lifecycles.
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:lifecycle:3.2.1'
}
```
## Usage
```kotlin
MaterialDialog(this).show {
...
lifecycleOwner(owner)
}
```
When the given lifecycle owner is destroyed, the dialog is automatically dismissed. Lifecycle
owners include Activities and Fragments from AndroidX, along with any class that implements the
`LifecycleOwner` interface.
# Material Dialogs
#### [View Releases and Changelogs](https://github.com/afollestad/material-dialogs/releases)
[![Android CI](https://github.com/afollestad/material-dialogs/workflows/Android%20CI/badge.svg)](https://github.com/afollestad/material-dialogs/actions?query=workflow%3A%22Android+CI%22)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/0a4acc30a9ce440087f7688735359bb8)](https://www.codacy.com/app/drummeraidan_50/material-dialogs?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=afollestad/material-dialogs&amp;utm_campaign=Badge_Grade)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
---
![Showcase](https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/showcase4.png)
# Modules
The core module is the fundamental module that you need in order to use this library. The others
are extensions to core.
Please note that since Material Dialogs 2.x.x, this library only supports Kotlin. The latest Java version is `0.9.6.0` and can be found [here](README_OLD.md). Note that 0.9.6.0 is unsupported, bugs & improvements will not be made to that version.
## Core
[ ![Core](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/core?label=core&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/core)
#### [Core Tutorial and Samples](documentation/CORE.md)
The `core` module contains everything you need to get started with the library. It contains all
core and normal-use functionality.
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/basic_with_buttons.png" width="250px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:core:3.3.0'
}
```
## Input
[ ![Input](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/input?label=input&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/input)
#### [Input Tutorial and Samples](documentation/INPUT.md)
The `input` module contains extensions to the core module, such as a text input dialog.
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/input.png" width="250px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:input:3.3.0'
}
```
## Files
[ ![Files](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/files?label=files&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/files)
#### [Files Tutorial and Samples](documentation/FILES.md)
The `files` module contains extensions to the core module, such as a file and folder chooser.
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/file_chooser.png" width="250px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:files:3.3.0'
}
```
## Color
[ ![Color](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/color?label=color&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/color)
#### [Color Tutorial and Samples](documentation/COLOR.md)
The `color` module contains extensions to the core module, such as a color chooser.
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/color_chooser.png" width="250px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:color:3.3.0'
}
```
## DateTime
[ ![DateTime](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/datetime?label=datetime&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/datetime)
#### [DateTime Tutorial and Samples](documentation/DATETIME.md)
The `datetime` module contains extensions to make date, time, and date-time picker dialogs.
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/datetimepicker.png" width="500px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:datetime:3.3.0'
}
```
## Bottom Sheets
[ ![Bottom Sheets](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/bottomsheets?label=bottomsheets&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/bottomsheets)
#### [Bottom Sheets Tutorial and Samples](documentation/BOTTOMSHEETS.md)
The `bottomsheets` module contains extensions to turn modal dialogs into bottom sheets, among
other functionality like showing a grid of items. Be sure to checkout the sample project for this,
too!
<img src="https://raw.githubusercontent.com/afollestad/material-dialogs/main/art/bottomsheet_customview.png" width="250px" />
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:bottomsheets:3.3.0'
}
```
## Lifecycle
[ ![Lifecycle](https://img.shields.io/maven-central/v/com.afollestad.material-dialogs/lifecycle?label=lifecycle&style=for-the-badge) ](https://repo1.maven.org/maven2/com/afollestad/material-dialogs/lifecycle)
#### [Lifecycle Tutorial and Samples](documentation/LIFECYCLE.md)
The `lifecycle` module contains extensions to make dialogs work with AndroidX lifecycles.
```gradle
dependencies {
...
implementation 'com.afollestad.material-dialogs:lifecycle:3.3.0'
}
```
此差异已折叠。
3.3.0
* Added `md_line_spacing_body` global theme attribute, which sets a global default for message line
spacing. See #1903.
* Added some assertions and sanity checks to avoid choice list adapter out of bounds crashes.
See #1906.
* Corner radius should not apply to the bottom of bottom sheet dialogs. See #1941.
* Fix dialog titles being cut off with custom fonts. See #1936.
* If `noVerticalPadding` is set with `customView(...)``, padding is not applied to the bottom of
the content `ScrollView` if `scrollable` is enabled. Resolves #1834.
* Input dialog styling is not enforced by the dialog. The global default for `TextInputLayout`
(`textInputStyle`) is used instead. See #1857.
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册