| | |
| | | package com.example.firstapp.activity |
| | | import android.app.AlertDialog |
| | | import android.os.Bundle |
| | | import android.widget.Toast |
| | | import androidx.appcompat.app.AppCompatActivity |
| | | import android.content.Intent |
| | | import android.graphics.Color |
| | | import android.text.SpannableString |
| | | import android.text.Spanned |
| | | import android.text.TextPaint |
| | | import android.text.method.LinkMovementMethod |
| | | import android.text.style.ClickableSpan |
| | | import android.view.LayoutInflater |
| | | import android.view.View |
| | | import android.widget.Button |
| | | import android.widget.CheckBox |
| | | import android.widget.TextView |
| | | import com.example.firstapp.R |
| | | import com.example.firstapp.databinding.ActivityLoginBinding |
| | | import com.example.firstapp.utils.PreferencesManager |
| | | import kotlin.system.exitProcess |
| | | |
| | | class LoginActivity : AppCompatActivity() { |
| | | private lateinit var binding: ActivityLoginBinding |
| | |
| | | binding = ActivityLoginBinding.inflate(layoutInflater) |
| | | setContentView(binding.root) |
| | | setupViews() |
| | | |
| | | val phone=PreferencesManager.getLastLoginPhone() |
| | | // 如果phone不存在则展示弹框 |
| | | if (phone.isNullOrEmpty()) { |
| | | showConfirmDialog() |
| | | } |
| | | } |
| | | |
| | | private fun setupViews() { |
| | |
| | | } |
| | | } |
| | | |
| | | private fun showConfirmDialog() { |
| | | val dialogView = LayoutInflater.from(this).inflate(R.layout.login_protocol_dialog_custom, null) |
| | | |
| | | val dialog = AlertDialog.Builder(this) |
| | | .setView(dialogView) |
| | | .create() |
| | | |
| | | dialog.window?.setBackgroundDrawableResource(R.drawable.dialog_background) |
| | | |
| | | val btnConfirm = dialogView.findViewById<Button>(R.id.btnConfirm) |
| | | btnConfirm.setOnClickListener { |
| | | dialog.dismiss() |
| | | } |
| | | val btnCancel = dialogView.findViewById<Button>(R.id.btnCancel) |
| | | btnCancel.setOnClickListener { |
| | | dialog.dismiss() |
| | | // 退出整个应用 |
| | | finishAffinity() // 关闭当前Activity和它之上的所有Activity |
| | | exitProcess(0) // 终止应用进程(需要引入 kotlin.system.exitProcess) |
| | | } |
| | | |
| | | // 👇 设置可点击文字逻辑 |
| | | val textView = dialogView.findViewById<TextView>(R.id.dialogMessage2) |
| | | val content = "您可以阅读「服务使用协议」和「隐私保护政策」了解相关信息。如果您同意,请点击同意,开始使用我们的服务。" |
| | | val spannableString = SpannableString(content) |
| | | |
| | | val serviceStart = content.indexOf("「服务使用协议」") |
| | | val serviceEnd = serviceStart + "「服务使用协议」".length |
| | | val privacyStart = content.indexOf("「隐私保护政策」") |
| | | val privacyEnd = privacyStart + "「隐私保护政策」".length |
| | | |
| | | spannableString.setSpan(object : ClickableSpan() { |
| | | override fun onClick(widget: View) { |
| | | startContentActivity("用户协议", "服务使用协议") |
| | | // Toast.makeText(this@LoginActivity, "点击了服务使用协议", Toast.LENGTH_SHORT).show() |
| | | // 可跳转 WebViewActivity 或打开链接 |
| | | } |
| | | |
| | | override fun updateDrawState(ds: TextPaint) { |
| | | super.updateDrawState(ds) |
| | | ds.color = Color.parseColor("#1E88E5") |
| | | ds.isUnderlineText = false |
| | | } |
| | | }, serviceStart, serviceEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) |
| | | |
| | | spannableString.setSpan(object : ClickableSpan() { |
| | | override fun onClick(widget: View) { |
| | | startContentActivity("隐私协议", "隐私保护政策") |
| | | // Toast.makeText(this@LoginActivity, "点击了隐私保护政策", Toast.LENGTH_SHORT).show() |
| | | } |
| | | |
| | | override fun updateDrawState(ds: TextPaint) { |
| | | super.updateDrawState(ds) |
| | | ds.color = Color.parseColor("#1E88E5") |
| | | ds.isUnderlineText = false |
| | | } |
| | | }, privacyStart, privacyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) |
| | | |
| | | textView.text = spannableString |
| | | textView.movementMethod = LinkMovementMethod.getInstance() |
| | | |
| | | dialog.show() |
| | | } |
| | | |
| | | |
| | | /* private fun showConfirmDialog() { |
| | | |
| | | val dialogView = LayoutInflater.from(this).inflate(R.layout.login_protocol_dialog_custom, null) |
| | | |
| | | val dialog = AlertDialog.Builder(this) |
| | | .setView(dialogView) |
| | | .create() |
| | | |
| | | // 设置自定义背景 |
| | | dialog.window?.setBackgroundDrawableResource(R.drawable.dialog_background) |
| | | |
| | | // 获取按钮并设置点击事件 |
| | | val btnConfirm = dialogView.findViewById<Button>(R.id.btnConfirm) |
| | | btnConfirm.setOnClickListener { |
| | | // 将协议勾选上 |
| | | dialog.dismiss() |
| | | } |
| | | |
| | | // 显示对话框 |
| | | dialog.show() |
| | | }*/ |
| | | |
| | | private fun startContentActivity(id: String, title: String) { |
| | | val intent = Intent(this, ContentDetailActivity::class.java).apply { |
| | | putExtra(ContentDetailActivity.ID, id) |
| | | putExtra(ContentDetailActivity.EXTRA_TITLE, title) |
| | | } |
| | | startActivity(intent) |
| | | } |
| | | |
| | | } |