【发布时间】:2018-06-04 09:10:54
【问题描述】:
我有一个包含多个 TextView's 的布局。当用户单击一个时,我检查TextView 是否有背景。如果它有背景我删除它,否则我设置它。
这是我的代码示例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linear"
android:orientation="vertical"
android:background="@drawable/selector">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="@android:style/TextAppearance.Medium"/>
</RelativeLayout>
所以我的问题是,如何检查TextView 是否已经设置了背景?
更新
if( textView.getBackground() == null) {
Log.i("here", "null");
textView.setBackgroundResource(R.drawable.draw_cross);
}
if(textView.getBackground() != null) {
Log.i("here", "not null");
textView.setBackground(null);
}
【问题讨论】:
-
不清楚您的问题中如何使用 ImageView。
-
我只是说我不想使用 ImageView。我想如果我们有办法检查 TextView 上是否有图标
-
我已经编辑了你的问题,如果我明白你想要什么帮助,接受它,我会给你答案!另外,请使用您在运行时使用的语言、Kotlin 或 Java 更新您的标签。
-
我批准了你的编辑。谢谢你把问题说清楚。你能给我答案吗?语言是 java
-
谢谢,没问题。我已经提交了对您问题的回答。