【问题标题】:android, how to format json string with some field boldandroid,如何用一些字段粗体格式化json字符串
【发布时间】:2021-04-22 16:48:31
【问题描述】:

在androidTextView中,当显示json字符串时,希望被正确格式化并且它应该有一些指定的字段粗体,例如:

{ “地点”: { "国家":"GB", “天气”:[ { "zip":20202, “描述”:“太阳”, “温度”:“80” } ] } },

显示为

试过@Zain 的解决方案,似乎不起作用(见下面的截图)。它在对话框的 TextView 中设置。没有缩进。

更新:它是对话框中的TextView

private fun showFormatedJsonString(context: Context, jsonStr: String) {
        val dialog = Dialog(context)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(R.layout.module_path)
        dialog.setCanceledOnTouchOutside(true)
        dialog.setCancelable(true)
        dialog.findViewById<TextView>(R.id.json_str).apply {
            
            text = jsonStr
        }
        dialog.findViewById<View>(R.id.root_container).setOnClickListener {
            dialog.dismiss()
        }
        
        val displayMetrics: DisplayMetrics = context.getResources().getDisplayMetrics()
        val dialogWidth = (displayMetrics.widthPixels * 0.85).toInt()
        val dialogHeight = (displayMetrics.heightPixels * 0.85).toInt()
        dialog.getWindow().setLayout(dialogWidth, dialogHeight)
        dialog.show()
    }

SpannableStringBuilder试过,也不起作用

//spStrBuilder.color ( Color.CYAN) { append("to be colored") }
                spStrBuilder.bold { append("to be bold") }

【问题讨论】:

    标签: android json kotlin textview


    【解决方案1】:

    如果匹配项多次出现,您可以通过编程方式执行此操作并使用SpannableString 用粗体标记文本

    String json = "{ \"location\": { \"country\":\"GB\", \"weather\":[ { \"zip\":20202, \"description\":\"sun\", \"temp\":\"80\" } ] } }";
    // List of words to be marked with bold
    List<String> boldList = Arrays.asList("country", "zip");
    final Spannable spannable = new SpannableString(json);
    
    // Finding match of words in the String
    for (String word : boldList) {
        int startIndex = json.indexOf(word);
        do {
            int endIndex = startIndex + word.length();
            spannable.setSpan(new StyleSpan(BOLD), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            startIndex = json.indexOf(word, endIndex);
        } while (startIndex != -1);
    }
    
    TextView textView = findViewById(R.id.foo);
    textView.setText(spannable);
    

    更新

    使用对话框中的代码:

    
    class MainActivity : AppCompatActivity() {
    
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.sometest8)
    
            val json =
                "{ \"location\": { \"country\":\"GB\", \"weather\":[ { \"zip\":20202, \"description\":\"sun\", \"temp\":\"80\" } ] } }"
            button.setOnClickListener {
                showFormatedJsonString(this, json)
            }
        }
    
    
        fun formatBold(json: String): Spannable {
            val boldList: List<String> = listOf("country", "zip")
            val spannable: Spannable = SpannableString(json)
    
            // Finding match of words in the String
            for (word in boldList) {
                var startIndex = json.indexOf(word)
                do {
                    val endIndex = startIndex + word.length
                    spannable.setSpan(
                        StyleSpan(Typeface.BOLD),
                        startIndex,
                        endIndex,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                    )
                    startIndex = json.indexOf(word, endIndex)
                } while (startIndex != -1)
            }
            return spannable
        }
    
    
        private fun showFormatedJsonString(context: Context, jsonStr: String) {
            val dialog = Dialog(context)
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
            dialog.setContentView(R.layout.module_path)
            dialog.setCanceledOnTouchOutside(true)
            dialog.setCancelable(true)
            dialog.findViewById<TextView>(R.id.json_str).apply {
                text = formatBold(jsonStr)
            }
            dialog.findViewById<View>(R.id.root_container).setOnClickListener {
                dialog.dismiss()
            }
    
            val displayMetrics: DisplayMetrics = context.getResources().getDisplayMetrics()
            val dialogWidth = (displayMetrics.widthPixels * 0.85).toInt()
            val dialogHeight = (displayMetrics.heightPixels * 0.85).toInt()
            dialog.getWindow()?.setLayout(dialogWidth, dialogHeight)
            dialog.show()
        }
    
    }
    

    预览

    【讨论】:

    • 谢谢,但它似乎不起作用。使用代码进行测试并使用屏幕截图更新问题。
    • 很抱歉听到......但你最初没有提到 textView 将在一个对话框中......我的共享代码在正常布局中工作并经过测试......所以我们需要知道如何你建立了对话
    • Dialog很简单,里面有一个TextView。
    • @lannyf 刚刚使用了你的代码,结果是一样的。请看看更新后的答案
    • 它确实对你有用,但不知道为什么我的没有显示粗体。现在相同的代码。会尝试更多。谢谢!
    【解决方案2】:

    您可以使用HtmlCompat.fromHtml( htmlTextHere, HtmlCompat.FROM_HTML_MODE_LEGACY ) 使用html标签(在您的情况下为&lt;b&gt;标签)对您的文本进行样式化,但是这样您必须准备您的文本,例如: "{\"location\": { &lt;b&gt;\"country\"&lt;/b&gt;:"+ object.location.country ...

    【讨论】:

    • 那会处理缩进格式吗?
    • 很遗憾没有,我尝试使用 &lt;pre&gt; 标签和不同的 HtmlCompat 常量,但我无法对其进行风格化
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    相关资源
    最近更新 更多