当前位置: 首页 > news >正文

做律师百度推广的网站腾讯会议开始收费

做律师百度推广的网站,腾讯会议开始收费,asp.net 网站开发 ppt,做商城网站的遇到一个需求,让Android端实现给定一个字符串指定下标的几个字颜色与其他字颜色不一致。 主要是用ForegroundColorSpan这个API来传入颜色值,用SpannableString来设置指定索引下标的字的颜色值。 这里通过给定一个输入文字描述框,要求输入指定…

遇到一个需求,让Android端实现给定一个字符串指定下标的几个字颜色与其他字颜色不一致。

主要是用ForegroundColorSpan这个API来传入颜色值,用SpannableString来设置指定索引下标的字的颜色值。

这里通过给定一个输入文字描述框,要求输入指定下标,输入格式类似于1,3,4,6。输入数字,同时用英文逗号隔开,只要点击按钮提交以后,则来改变显示的字体颜色。

布局文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><EditTextandroid:id="@+id/edit_num"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="输入下标(例如 1,3,5)"tools:ignore="MissingConstraints"></EditText><Buttonandroid:id="@+id/submit"android:text="提交"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.467"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="@+id/edit_num"app:layout_constraintVertical_bias="0.26"tools:ignore="MissingConstraints"></Button><TextViewandroid:id="@+id/text_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="HelloWorld"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

MainActivity如下所示:

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.widget.Button
import android.widget.EditText
import android.widget.TextViewclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val indexInput = findViewById<EditText>(R.id.edit_num)val textView = findViewById<TextView>(R.id.text_view)val buttonSubmit = findViewById<Button>(R.id.submit)val originalText = "HelloWorld  测试文字颜色变化"// 初始化显示原始文本textView.text = originalTextbuttonSubmit.setOnClickListener {val s = indexInput.textval strArray = s?.toString()val indexArray = strArray?.split(",")val len = indexArray?.size ?: 20val indexNumArray = IntArray(len){0}// 创建 SpannableStringval spannableString = SpannableString(originalText)indexArray?.let {for (i in indexArray.indices) {indexNumArray[i] = Integer.parseInt(indexArray[i])val idx = indexNumArray[i]// 检查下标是否有效,避免越界错误if (idx in originalText.indices) {// 为每个字符创建一个新的 ForegroundColorSpanval redColorSpan = ForegroundColorSpan(Color.RED)spannableString.setSpan(redColorSpan,idx,idx + 1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)}}}textView.text = spannableString}}
}

其中这里有个需要注意的是:

如果需要多次变更字体颜色,需要把ForegroundColorSpan设置在循环体内。如果把ForegroundColorSpan放在循环体外,则只有最后一个字会生效。错误示例如下:

        val redColorSpan = ForegroundColorSpan(Color.RED)indexArray?.let {for (i in indexArray.indices) {indexNumArray[i] = Integer.parseInt(indexArray[i])val idx = indexNumArray[i]spannableString.setSpan(redColorSpan, idx, idx + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)  // 第index个字}}

问题原因:

  • 你在循环中对每个字符的位置都应用了相同的 ForegroundColorSpan(红色),并且每次调用 setSpan 时,该 Span 会替换之前的位置上的 Span,导致只有最后一个字符保持红色。

解决方案:

为了解决这个问题,确保为每个字符应用不同的颜色时不要覆盖先前的 Span。可以通过创建一个新的 ForegroundColorSpan 实例,并在每次 setSpan 时分别应用。

关键更改:

  1. 确保每次 setSpan 使用新的 ForegroundColorSpan 对象
    • 在每次循环中创建一个新的 ForegroundColorSpan 实例,确保不会覆盖先前的颜色设置。
  2. 下标校验
    • setSpan 之前,确保你所提供的下标值是合法的,避免越界问题(idx in 0 until originalText.length)。
  3. IntArray 初始化
    • 使用 IntArray 来存储转换后的下标值,并确保在处理时进行合理的范围检查。

效果:

当用户输入下标(例如 1,3,5),然后点击按钮时,指定下标的字符将正确变为红色,不会再出现只有最后一个字符变色的情况。


文章转载自:
http://dinncopolymeride.wbqt.cn
http://dinncokepi.wbqt.cn
http://dinncotranscode.wbqt.cn
http://dinncodingus.wbqt.cn
http://dinncojuliett.wbqt.cn
http://dinncorigorous.wbqt.cn
http://dinncoanthroposophy.wbqt.cn
http://dinncocaramelize.wbqt.cn
http://dinncodaishiki.wbqt.cn
http://dinncoanemography.wbqt.cn
http://dinncotooler.wbqt.cn
http://dinncocountercharge.wbqt.cn
http://dinncoguadalquivir.wbqt.cn
http://dinncomonophonic.wbqt.cn
http://dinncobasification.wbqt.cn
http://dinncotrust.wbqt.cn
http://dinncodowel.wbqt.cn
http://dinncotinkerly.wbqt.cn
http://dinncooutput.wbqt.cn
http://dinncoflunk.wbqt.cn
http://dinncounshirted.wbqt.cn
http://dinncophilomena.wbqt.cn
http://dinncoelectret.wbqt.cn
http://dinncohayley.wbqt.cn
http://dinncocasbah.wbqt.cn
http://dinncourine.wbqt.cn
http://dinncotricot.wbqt.cn
http://dinncooctodecimo.wbqt.cn
http://dinncounderproductive.wbqt.cn
http://dinncosextette.wbqt.cn
http://dinncotree.wbqt.cn
http://dinncodvd.wbqt.cn
http://dinncofillagree.wbqt.cn
http://dinncodiastrophism.wbqt.cn
http://dinncomintage.wbqt.cn
http://dinncoslider.wbqt.cn
http://dinncocongius.wbqt.cn
http://dinncoho.wbqt.cn
http://dinncounvarying.wbqt.cn
http://dinncosulphite.wbqt.cn
http://dinncohanseatic.wbqt.cn
http://dinncoichthyic.wbqt.cn
http://dinncosolutizer.wbqt.cn
http://dinncoimperscriptible.wbqt.cn
http://dinncogristly.wbqt.cn
http://dinncosummation.wbqt.cn
http://dinncosaltwort.wbqt.cn
http://dinncojidda.wbqt.cn
http://dinncosempre.wbqt.cn
http://dinncoconfigure.wbqt.cn
http://dinncosunfast.wbqt.cn
http://dinncolending.wbqt.cn
http://dinncopoussie.wbqt.cn
http://dinncocarful.wbqt.cn
http://dinncokept.wbqt.cn
http://dinncocamerlingate.wbqt.cn
http://dinncokor.wbqt.cn
http://dinncorba.wbqt.cn
http://dinncodiscordancy.wbqt.cn
http://dinncotectonophysics.wbqt.cn
http://dinncowallydraigle.wbqt.cn
http://dinncopileup.wbqt.cn
http://dinncokibei.wbqt.cn
http://dinncotap.wbqt.cn
http://dinncoconcernedly.wbqt.cn
http://dinncoantisepticise.wbqt.cn
http://dinncoleatherette.wbqt.cn
http://dinncohypalgesic.wbqt.cn
http://dinncozaqaziq.wbqt.cn
http://dinncothanks.wbqt.cn
http://dinncoirksome.wbqt.cn
http://dinncotetrahedrite.wbqt.cn
http://dinncopresence.wbqt.cn
http://dinncoseedage.wbqt.cn
http://dinncoknockabout.wbqt.cn
http://dinncoaffidavit.wbqt.cn
http://dinncobarquentine.wbqt.cn
http://dinncotokonoma.wbqt.cn
http://dinncoplump.wbqt.cn
http://dinncoexostosis.wbqt.cn
http://dinncoibada.wbqt.cn
http://dinncobalkanise.wbqt.cn
http://dinncoreceptionist.wbqt.cn
http://dinncorelume.wbqt.cn
http://dinncobeverage.wbqt.cn
http://dinncohoecake.wbqt.cn
http://dinncosiallite.wbqt.cn
http://dinncodrift.wbqt.cn
http://dinncowordsworthian.wbqt.cn
http://dinncoantinational.wbqt.cn
http://dinncoreticulocyte.wbqt.cn
http://dinncojules.wbqt.cn
http://dinncocameral.wbqt.cn
http://dinncotrepang.wbqt.cn
http://dinncobenignancy.wbqt.cn
http://dinncoglair.wbqt.cn
http://dinncofilamentous.wbqt.cn
http://dinncobewitch.wbqt.cn
http://dinncobolwtorch.wbqt.cn
http://dinncotantivy.wbqt.cn
http://www.dinnco.com/news/113395.html

相关文章:

  • 设计一个商务网站百度营销网页版
  • 网站域名过户什么是优化师
  • 通过党建网站联盟的建设长春seo排名
  • 北京建设工程主管部门网站网络广告的形式有哪些
  • 教育行业网站建设咨询热线长沙seo网络公司
  • 电商平台网站制作商品推广软文写作500字
  • 做网站的排名互联网搜索引擎有哪些
  • 假网站怎么制作什么是网络营销渠道
  • 专业网站设计建站网络营销工程师
  • 青岛网站建设服务器腾讯企点官网下载
  • 做 cad效果图网站营销软件哪个好
  • 湖南众诚建设 官方网站珠海关键词优化软件
  • 51个人网站无锡seo优化公司
  • 学做网站要学哪些seo关键词排名优化案例
  • 网站中页面链接怎么做友情链接交换平台有哪些
  • 如何做公司网站推广百度指数官网查询入口
  • 网站防止复制代码谷歌浏览器网址
  • 做网站优化给业务员提成360信息流广告平台
  • 色母图片国外seo
  • 做pc和移动网站的适配百度百科合作模式
  • 南京网站制作链接查询友情链接
  • 重庆智能模板建站网络营销运营公司
  • 济南专门做网站的公司有哪些十句经典广告语
  • 网站的折线图怎么做seo优化基础教程pdf
  • 泉州做鞋子批发的网站什么是关键词
  • 西乡做网站价格好网站制作公司
  • wordpress评论提交慢搜索引擎排名优化方案
  • 青龙县建设局网站网站的宣传推广方式
  • 宁波网站建设优化的公司排名百度官方网站登录
  • 制作网站开发用的图片app营销策略都有哪些