如何检查 TextView 在 android 中是否有图像
how to check if TextView has image in android
我的布局有多个 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);
}
您可以使用
获取Background-Drawable
myTextView.getBackground();
如果为空,则不设置背景,否则为。
然后你可以这样做:
if(myTextView.getBackground() == null)
myTextView.setBackground(myDrawable)
else
myTextView.setBackground(null)
您可以使用以下代码实现:
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 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);
}
}
});
我的布局有多个 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);
}
您可以使用
获取Background-DrawablemyTextView.getBackground();
如果为空,则不设置背景,否则为。 然后你可以这样做:
if(myTextView.getBackground() == null)
myTextView.setBackground(myDrawable)
else
myTextView.setBackground(null)
您可以使用以下代码实现:
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 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);
}
}
});