【问题标题】:how to check if TextView has image in android如何检查TextView是否在android中有图像
【发布时间】: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
  • 谢谢,没问题。我已经提交了对您问题的回答。

标签: java android xml textview


【解决方案1】:

您可以使用以下代码实现此目的:

Java:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.text_view);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (hasBackground(v)) {
                    v.setBackground(null);
                } else {
                    v.setBackgroundResource(R.drawable.draw_cross);
                }
            }
        });
    }

    private boolean hasBackground(View v) {
        return v.getBackground() != null;
    }
}

【讨论】:

  • 感谢您的帮助。我现在尝试了您的代码,但是当 textView 为空时,setBackgroundResource 没有设置图像。我更新了我的问题,请您看看代码有什么问题?
  • 那是我的错误。感谢您的帮助。我得到了解决方案。
  • 不用担心,很高兴我能提供帮助!为了以防万一,我用一个完整的活动示例更新了我的答案。
【解决方案2】:

您可以通过使用获取 Background-Drawable

myTextView.getBackground();

如果为null,则不设置背景,否则设置。 然后你可以这样做:

if(myTextView.getBackground() == null)
    myTextView.setBackground(myDrawable)
else
    myTextView.setBackground(null)

【讨论】:

  • 谢谢你的回答,我也试过代码,但没有图像到TextView
【解决方案3】:

这绝对能满足你的要求-

TextView tv = findViewById(R.id.text_view);
int count = 1;

tv.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        if(tv.getBackground() == null)
        {
            tv.setBackgroundResource(R.drawable.draw_cross);
            count = count+2;
        }
        else if(count%2 != 0)
        {
            tv.setBackgroundResource(0);
        }
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-15
    • 2013-03-12
    • 2014-12-15
    • 2013-02-03
    • 2017-10-13
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    相关资源
    最近更新 更多