zhujie
2025-04-03 fe04012057d024770e0180543483d393281a542f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.example.firstapp.activity
 
import android.os.Bundle
import android.webkit.WebView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.example.firstapp.R
import com.example.firstapp.database.service.RetrofitClient
import kotlinx.coroutines.launch
 
class ContentDetailActivity : AppCompatActivity() {
    private lateinit var webView: WebView
    private lateinit var toolbar: androidx.appcompat.widget.Toolbar
 
    companion object {
        const val ID = "id"
        const val EXTRA_TITLE = "title"
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_content_detail)
 
        // 初始化视图
        webView = findViewById(R.id.webView)
        toolbar = findViewById(R.id.toolbar)
 
        // 设置标题
        val title = intent.getStringExtra(EXTRA_TITLE) ?: "内容详情"
        toolbar.title = title
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        toolbar.setNavigationOnClickListener { finish() }
 
        // 获取内容类型
        val id = intent.getStringExtra(ID)
        if (id != null) {
            loadContent(id)
        } else {
            Toast.makeText(this, "参数错误", Toast.LENGTH_SHORT).show()
            finish()
        }
    }
 
    private fun loadContent(id: String) {
        lifecycleScope.launch {
            try {
                val response = RetrofitClient.apiService.getContentById(id)
                if (response.code == "0" && response.data != null) {
                    // 构建HTML内容
                    val htmlContent = """
                        <!DOCTYPE html>
                        <html>
                        <head>
                            <meta name="viewport" content="width=device-width, initial-scale=1">
                            <style>
                                body { 
                                    padding: 16px; 
                                    font-size: 16px; 
                                    line-height: 1.5;
                                }
                            </style>
                        </head>
                        <body>
                            ${response.data.content}
                        </body>
                        </html>
                        
                        
                        
                        
                    """.trimIndent()
 
                    webView.loadDataWithBaseURL(null, htmlContent, "text/html", "UTF-8", null)
                } else {
                    Toast.makeText(this@ContentDetailActivity, "获取内容失败", Toast.LENGTH_SHORT).show()
                }
            } catch (e: Exception) {
                Toast.makeText(this@ContentDetailActivity, "网络错误", Toast.LENGTH_SHORT).show()
            }
        }
    }
}