CommonUI 模块是公共UI组件库,提供了项目中常用的自定义视图组件、通用布局、动画效果等UI相关功能。该模块旨在统一UI风格,提高开发效率,确保用户体验的一致性。
class CommonButton : Button {
enum class ButtonStyle {
PRIMARY, // 主要按钮
SECONDARY, // 次要按钮
OUTLINE, // 边框按钮
TEXT // 文本按钮
}
fun setButtonStyle(style: ButtonStyle)
fun setLoading(loading: Boolean)
fun setEnabled(enabled: Boolean)
}
class CommonEditText : EditText {
fun setHint(hint: String)
fun setError(error: String?)
fun setMaxLength(maxLength: Int)
fun addTextChangedListener(listener: TextWatcher)
}
class CommonDialog {
fun setTitle(title: String): CommonDialog
fun setMessage(message: String): CommonDialog
fun setPositiveButton(text: String, listener: OnClickListener): CommonDialog
fun setNegativeButton(text: String, listener: OnClickListener): CommonDialog
fun show()
fun dismiss()
}
<!-- 通用按钮 -->
<com.adealink.frame.commonui.CommonButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定"
app:button_style="primary"
app:loading="false" />
<!-- 输入框 -->
<com.adealink.frame.commonui.CommonEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
app:max_length="100" />
<!-- 卡片布局 -->
<com.adealink.frame.commonui.CardLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:card_corner_radius="8dp"
app:card_elevation="4dp">
<!-- 内容 -->
</com.adealink.frame.commonui.CardLayout>
// 创建对话框
CommonDialog.Builder(context)
.setTitle("提示")
.setMessage("确定要删除吗?")
.setPositiveButton("确定") { dialog, _ ->
// 确定操作
dialog.dismiss()
}
.setNegativeButton("取消") { dialog, _ ->
dialog.dismiss()
}
.show()
// 设置按钮状态
button.setButtonStyle(CommonButton.ButtonStyle.PRIMARY)
button.setLoading(true)
// 输入框验证
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
val text = s?.toString() ?: ""
if (text.length > 50) {
editText.setError("输入内容过长")
} else {
editText.setError(null)
}
}
})
<!-- styles.xml -->
<style name="CommonButtonPrimary">
<item name="android:background">@drawable/button_primary_bg</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">16sp</item>
<item name="android:padding">12dp</item>
</style>
<style name="CommonCard">
<item name="cardCornerRadius">8dp</item>
<item name="cardElevation">4dp</item>
<item name="cardBackgroundColor">@color/white</item>
</style>
<!-- attrs.xml -->
<declare-styleable name="CommonButton">
<attr name="button_style" format="enum">
<enum name="primary" value="0" />
<enum name="secondary" value="1" />
<enum name="outline" value="2" />
<enum name="text" value="3" />
</attr>
<attr name="loading" format="boolean" />
<attr name="corner_radius" format="dimension" />
</declare-styleable>
<declare-styleable name="CommonEditText">
<attr name="max_length" format="integer" />
<attr name="error_text" format="string" />
<attr name="required" format="boolean" />
</declare-styleable>
// 页面切换动画
AnimationUtils.slideInFromRight(context, view)
AnimationUtils.slideOutToLeft(context, view)
AnimationUtils.fadeIn(context, view, duration)
AnimationUtils.fadeOut(context, view, duration)
// 显示加载动画
LoadingView.show(context, "加载中...")
LoadingView.dismiss()
// 刷新动画
refreshLayout.setRefreshing(true)
refreshLayout.setOnRefreshListener {
// 刷新逻辑
refreshLayout.setRefreshing(false)
}
androidx.appcompat - AppCompat 支持androidx.core.ktx - Android 核心扩展androidx.constraintlayout - 约束布局com.google.android.material - Material Design 组件frame:base - 基础框架模块frame:util - 工具类模块implementation "com.wenext.android:frame-commonui:6.0.0"