优化
This commit is contained in:
@@ -5,6 +5,8 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
|||||||
import com.shuwei.intelligent.shelves.adapter.LogAdapter
|
import com.shuwei.intelligent.shelves.adapter.LogAdapter
|
||||||
import com.shuwei.intelligent.shelves.base.BaseActivity
|
import com.shuwei.intelligent.shelves.base.BaseActivity
|
||||||
import com.shuwei.intelligent.shelves.databinding.ActivityLogListBinding
|
import com.shuwei.intelligent.shelves.databinding.ActivityLogListBinding
|
||||||
|
import com.shuwei.intelligent.shelves.dialog.CommonDialog
|
||||||
|
import com.shuwei.intelligent.shelves.utils.ext.copyTextToClipboard
|
||||||
import com.shuwei.intelligent.shelves.utils.ext.startActivity
|
import com.shuwei.intelligent.shelves.utils.ext.startActivity
|
||||||
|
|
||||||
class LogActivity : BaseActivity() {
|
class LogActivity : BaseActivity() {
|
||||||
@@ -22,6 +24,7 @@ class LogActivity : BaseActivity() {
|
|||||||
LogAdapter(logList).apply {
|
LogAdapter(logList).apply {
|
||||||
setOnItemClickListener { _, _, position ->
|
setOnItemClickListener { _, _, position ->
|
||||||
if (isLogDir.not()) {
|
if (isLogDir.not()) {
|
||||||
|
showConfirmDialog(logList[position])
|
||||||
return@setOnItemClickListener
|
return@setOnItemClickListener
|
||||||
}
|
}
|
||||||
startActivity<LogActivity> {
|
startActivity<LogActivity> {
|
||||||
@@ -32,6 +35,16 @@ class LogActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun showConfirmDialog(content:String) {
|
||||||
|
CommonDialog(this).apply {
|
||||||
|
dialogTitle = "温馨提示"
|
||||||
|
dialogContent = content
|
||||||
|
confirmBlock = {
|
||||||
|
content?.copyTextToClipboard(this@LogActivity)
|
||||||
|
}
|
||||||
|
}.show()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
binding = ActivityLogListBinding.inflate(layoutInflater)
|
binding = ActivityLogListBinding.inflate(layoutInflater)
|
||||||
@@ -41,6 +54,10 @@ class LogActivity : BaseActivity() {
|
|||||||
logDate = intent.getStringExtra(LOG_DATE) ?: ""
|
logDate = intent.getStringExtra(LOG_DATE) ?: ""
|
||||||
binding.ivBack.setOnClickListener { finish() }
|
binding.ivBack.setOnClickListener { finish() }
|
||||||
binding.tvTitle.text = if (isLogDir) "全部日志" else logDate
|
binding.tvTitle.text = if (isLogDir) "全部日志" else logDate
|
||||||
|
binding.tvTitle.setOnClickListener {
|
||||||
|
logList.reverse()
|
||||||
|
logAdapter.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
if (isLogDir) {
|
if (isLogDir) {
|
||||||
logList.addAll(fileLogger.getLogFiles())
|
logList.addAll(fileLogger.getLogFiles())
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.shuwei.intelligent.shelves.base
|
||||||
|
|
||||||
|
import android.app.Dialog
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.drawable.ColorDrawable
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.Gravity
|
||||||
|
import android.view.View
|
||||||
|
import com.shuwei.intelligent.shelves.R
|
||||||
|
import com.shuwei.intelligent.shelves.utils.ext.dp
|
||||||
|
import com.shuwei.intelligent.shelves.utils.ext.hideKeyboard
|
||||||
|
|
||||||
|
abstract class BaseDialog(
|
||||||
|
context: Context,
|
||||||
|
var defWidth: Int = 400.dp,
|
||||||
|
var defHeight: Int = 300.dp
|
||||||
|
) : Dialog(context, R.style.DialogTheme) {
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
val rootView = getRootView()
|
||||||
|
setContentView(rootView)
|
||||||
|
setCancelable(false)
|
||||||
|
setCanceledOnTouchOutside(false)
|
||||||
|
window?.apply {
|
||||||
|
setLayout(defWidth, defHeight)
|
||||||
|
setBackgroundDrawable(ColorDrawable())
|
||||||
|
setGravity(Gravity.CENTER)
|
||||||
|
}
|
||||||
|
rootView.setOnClickListener { it.hideKeyboard() }
|
||||||
|
initView()
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract fun getRootView(): View
|
||||||
|
|
||||||
|
abstract fun initView()
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.shuwei.intelligent.shelves.dialog
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import com.shuwei.intelligent.shelves.base.BaseDialog
|
||||||
|
import com.shuwei.intelligent.shelves.databinding.DialogLayoutBinding
|
||||||
|
|
||||||
|
class CommonDialog(context: Context):BaseDialog(context) {
|
||||||
|
|
||||||
|
private lateinit var binding:DialogLayoutBinding
|
||||||
|
|
||||||
|
override fun getRootView(): View {
|
||||||
|
binding = DialogLayoutBinding.inflate(LayoutInflater.from(context))
|
||||||
|
return binding.root
|
||||||
|
}
|
||||||
|
|
||||||
|
var dialogTitle:String?=null
|
||||||
|
var dialogContent:String?=null
|
||||||
|
var confirmBlock:(()->Unit)?=null
|
||||||
|
override fun initView() {
|
||||||
|
binding.btnConfirm.setOnClickListener {
|
||||||
|
confirmBlock?.invoke()
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun show() {
|
||||||
|
super.show()
|
||||||
|
binding.tvDialogTitle.text = dialogTitle
|
||||||
|
binding.tvDialogContent.text = dialogContent
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import android.os.Bundle
|
|||||||
import android.util.TypedValue
|
import android.util.TypedValue
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.inputmethod.EditorInfo
|
import android.view.inputmethod.EditorInfo
|
||||||
|
import android.view.inputmethod.InputMethodManager
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.core.app.ActivityOptionsCompat
|
import androidx.core.app.ActivityOptionsCompat
|
||||||
@@ -22,6 +23,8 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import android.content.ClipData
|
||||||
|
import android.content.ClipboardManager
|
||||||
|
|
||||||
fun Context.toast(
|
fun Context.toast(
|
||||||
message: String?,
|
message: String?,
|
||||||
@@ -151,3 +154,24 @@ fun View.clickWithDebounce(delay: Long = 500, action: () -> Unit) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun View.hideKeyboard() {
|
||||||
|
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
|
imm.hideSoftInputFromWindow(this.windowToken, 0)
|
||||||
|
this.clearFocus() // 清除焦点避免键盘再次弹出
|
||||||
|
}
|
||||||
|
|
||||||
|
fun String.copyTextToClipboard(context: Context) {
|
||||||
|
// 获取系统服务中的ClipboardManager
|
||||||
|
val clipboard: ClipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||||
|
|
||||||
|
// 创建一个ClipData对象,包含要复制的文本
|
||||||
|
val clip = ClipData.newPlainText("label", this)
|
||||||
|
|
||||||
|
// 将ClipData对象设置到剪贴板
|
||||||
|
clipboard.setPrimaryClip(clip)
|
||||||
|
|
||||||
|
// 显示一个简单的Toast提示用户文本已复制
|
||||||
|
Toast.makeText(context, "文本已复制到剪贴板", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="400dp"
|
||||||
|
android:layout_height="300dp"
|
||||||
|
app:cardCornerRadius="10dp"
|
||||||
|
app:cardElevation="0dp"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
app:cardBackgroundColor="@color/white">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_marginVertical="30dp"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvDialogTitle"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center_horizontal"
|
||||||
|
android:textColor="@color/black"
|
||||||
|
android:textSize="22sp"
|
||||||
|
tools:text="标题"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvDialogContent"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:layout_gravity="center_horizontal"
|
||||||
|
android:textColor="@color/black"
|
||||||
|
android:gravity="center"
|
||||||
|
android:layout_marginHorizontal="50dp"
|
||||||
|
android:textSize="16sp"
|
||||||
|
tools:text="内容"/>
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatButton
|
||||||
|
android:id="@+id/btnConfirm"
|
||||||
|
android:layout_width="120dp"
|
||||||
|
android:layout_height="50dp"
|
||||||
|
android:text="确定"
|
||||||
|
android:background="@color/bg_card_blue"
|
||||||
|
android:textColor="@color/white"
|
||||||
|
android:textSize="20sp"
|
||||||
|
android:layout_gravity="center_horizontal"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</androidx.cardview.widget.CardView>
|
||||||
@@ -19,4 +19,24 @@
|
|||||||
<item name="android:windowIsFloating">true</item>
|
<item name="android:windowIsFloating">true</item>
|
||||||
<item name="android:windowBackground">@android:color/transparent</item>
|
<item name="android:windowBackground">@android:color/transparent</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<style name="DialogTheme" parent="@android:style/Theme.Dialog">
|
||||||
|
<!-- 边框 -->
|
||||||
|
<item name="android:windowFrame">@null</item>
|
||||||
|
<!-- 是否浮现在activity之上 -->
|
||||||
|
<item name="android:windowIsFloating">true</item>
|
||||||
|
<!-- 半透明 -->
|
||||||
|
<item name="android:windowIsTranslucent">true</item>
|
||||||
|
<!-- 无标题 -->
|
||||||
|
<item name="android:windowNoTitle">true</item>
|
||||||
|
<item name="android:background">@android:color/transparent</item>
|
||||||
|
<!-- 背景透明 -->
|
||||||
|
<item name="android:windowBackground">@android:color/transparent</item>
|
||||||
|
<!-- 模糊 -->
|
||||||
|
<item name="android:backgroundDimEnabled">true</item>
|
||||||
|
<!-- 遮罩层 -->
|
||||||
|
<item name="android:backgroundDimAmount">0.5</item>
|
||||||
|
</style>
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user