Tag: textview

TextView在设计预览中显示,但不在实际的Android应用程序中显示

我在第二个Activity中有一个XML布局,根据用户是否登录来显示两个LinearLayouts的一个,而TextView我从来没有在应用中显示文本,但是当我看到布局编辑器中的“设计”面板 TextView在那里(如果我设置背景颜色,它显示颜色),但文本本身从不呈现。 单击MainActivity上的一个按钮将导致启动具有onCreate (它是Kotlin)的LoginActivity : override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.login_layout) 和布局: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="gone" android:id="@+id/loggedOut" android:padding="24dp"> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/passName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:focusable="true" android:hint="Username" android:inputType="textCapCharacters" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/passPin" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:focusable="true" android:hint="PIN" android:inputType="numberPassword" /> </android.support.design.widget.TextInputLayout> <Button […]

将textView与Kotlin中的字符串进行比较

我试图改变我的Android应用程序的文本视图每次我按下按钮 问题在于“if”操作不适用,就好像它总是返回false 这是为什么 ? override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val lol: String = "Hello World!" button.setOnClickListener{ if ( textview.equals(lol)){ textview.setText("yeah")} else textview.setText("Hello World!") }}}

Android Kotlin`.replaceRange`不会替换SpannableString中的文本

我的Android应用程序使用ClickableSpan处理扰流标签。 我想在TextView为它实现文本替换,但是.replaceRange什么都不做。 我可以确认startPos和endPos是有效的位置。 var text = ((textView as TextView).text as SpannableString) var spans = text.getSpans(0, text.length, SpoilerSpan::class.java) for (span in spans) { if (span == this) { val startPos = text.getSpanStart(span) val endPos = text.getSpanEnd(span) printout(startPos.toString() + " " + endPos.toString()) text.replaceRange(startPos, endPos, spoilerText) printout(text.toString()) } } textView.text = text

Kotlin:如何使用Kotlin在Android中将文本设置为TextView?

我是新手。 我想改变TextView文本,而点击它。 我的代码: val text: TextView = findViewById(R.id.android_text) as TextView text.setOnClickListener { text.setText(getString(R.string.name)) } 输出:我得到的输出,但显示使用属性访问语法 。 谁能告诉我该怎么做? 提前致谢。

如何在Kotlin for Android上使用“setTextColor(hexaValue)”,

背景 在Java中,我可以使用它的标准十六进制值直接更改TextView的文本颜色: textView.setTextColor(0xffffffff); //white textView.setTextColor(0x00000000); //transparent textView.setTextColor(0xff000000); //black textView.setTextColor(0xff0000ff); //blue //etc… 好简单… 问题 在Kotlin上,如果我尝试写这样的东西,我得到一个奇怪的构建错误: 错误:(15,18)以下函数都不能用所提供的参数调用:public open fun setTextColor(p0:ColorStateList!):在android.widget.TextView中定义的单位public open fun setTextColor(p0:Int):Unit在android.widget.TextView中定义 我试过了 我试图通过互联网搜索这个,我看不出有什么特别的十六进制值。 看起来像在Java上一样: https://kotlinlang.org/docs/reference/basic-types.html 然后我决定用Java编写,转换成Kotlin。 结果在颜色值方面是非常不可读的: textView.setTextColor(-0x1) //white textView.setTextColor(0x00000000) //transparent textView.setTextColor(-0x1000000) //black textView.setTextColor(-0xffff01) //blue 对我来说,用于Kotlin的Integer的十六进制值似乎是有符号的,而在Java上它会自动转换为带符号的值,所以这会导致值的翻转以及需要时设置一个负号。 我能想到的唯一的东西,仍然允许阅读,就像这样: textView.setTextColor(Integer.parseUnsignedInt("ffff0000",16)); 但是,这有多个缺点: 这是更长的时间。 它转换一个字符串,所以效率要低得多 最重要的是:它仅适用于API 26(Android O),它目前在全球约1%的Android设备上处于活动状态。 问题 为什么会发生? 我能做些什么才能使其具有最好的可读性,无需字符串转换,并且可以在所有Android版本上运行(在我的情况下,minSdkVersion 14)?