有没有一种方法可以更改 Android 中的所有 TextView?

huangapple go评论133阅读模式
英文:

Is there a way to change all TextViews in Android?

问题

我试图让我的textView在使用以下代码后在4秒内变为不可见,然后再出现:

final TextView textView233 = findViewById(R.id.textView233);
textView233.setText("加载数据中...");
textView233.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        textView233.setVisibility(View.INVISIBLE);
    }
}, 4000);

在我活动中有超过100个textView的情况下,我希望所有的textView都能执行相同的操作,是否有一种方法可以应用于所有的textView,而不必为所有100个textView编写代码?任何帮助将不胜感激,谢谢!

英文:

I'm attempting to make my textView invisible for 4 seconds and then appear after using the code:

final TextView textView233 = findViewById(R.id.textView233);
    textView233.setText("Loading data...");
    textView233.setVisibility(View.VISIBLE);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            textView233.setVisibility(View.INVISIBLE);
        }
    }, 4000);

In the situation where I have over 100 textViews in my activity and I want ALL textviews to do the same, is there a way to apply it to all textViews other than writing the code for all 100 of them? Any help would be appreciated, thanks!

答案1

得分: 2

将所有这些TextView添加到一个LinearLayout中,并在给定的间隔之后隐藏LinearLayout布局,而不是单独隐藏每个TextView。

英文:

add all these TextView inside some LinearLayout and hide the LinearLayout layout after the given internal instead of hiding each and every TextView separately.

答案2

得分: 0

你可以使用自定义类来实现:

AnimTextView.java

import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;

public class AnimTextView {

    private ArrayList<TextView> textViewList;

    public AnimTextView(TextView ... textViews) {
        textViewList = new ArrayList<>();
        textViewList.addAll(Arrays.asList(textViews));
    }

    public void startLoading(){
        for (final TextView textView: textViewList) {
            textView.setText("加载中..."); // 设置文本为 "加载中..."
            textView.setVisibility(View.VISIBLE); // 设置可见性为可见
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    textView.setVisibility(View.INVISIBLE); // 4秒后设置可见性为不可见
                }
            }, 4000);
        }
    }
}

现在,在你的主活动中:

AnimTextView animTextView = new AnimTextView(textView1, textView2, textView3, textView4, ...);

当你需要开始加载时:

animTextView.startLoading();
英文:

You can do it with a custom class:

AnimTextView.java

import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;

public class AnimTextView {

private ArrayList&lt;TextView&gt; textViewList;

public AnimTextView(TextView ... textViews) {
    textViewList = new ArrayList&lt;&gt;();
    textViewList.addAll(Arrays.asList(textViews));
}

public void startLoading(){
    for (final TextView textView: textViewList) {
        textView.setText(&quot;Loading data...&quot;);
        textView.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                textView.setVisibility(View.INVISIBLE);
            }
        }, 4000);
    }
}
}

Now, In your Main Activity:

AnimTextView animTextView = new AnimTextView(textView1, textView2, textView3, textView4, ...);

And when you need to start loading:

animTextView.startLoading();

答案3

得分: 0

以下是函数,它将获取活动中的所有TextView(包括嵌套的TextView)并隐藏它们,您可以从您的Handler中调用它:

fun recursiveHideTextAllViews(parent: ViewGroup?) {
    for (i in 0 until parent!!.childCount) {
        val child: View? = parent.getChildAt(i)
        if (child is ViewGroup) {
            recursiveHideTextAllViews(child as ViewGroup?)
        } else if (child!!.javaClass == AppCompatTextView::class.java) {
            child.visibility = View.INVISIBLE;
        }
    }
}

parent 是您的根视图。

英文:

Here is function that will get all TextViews of the Activity(even nested) and hide, you can call it from your Handler:

fun recursiveHideTextAllViews(parent: ViewGroup?) {
    for (i in 0 until parent!!.childCount) {
        val child: View? = parent.getChildAt(i)
        if (child is ViewGroup) {
            recursiveHideTextAllViews(child as ViewGroup?)
        } else if (child!!.javaClass == AppCompatTextView::class.java) {
            child.visibility = View.INVISIBLE;
        }
    }
}

parent is your Root view

huangapple
  • 本文由 发表于 2020年8月8日 16:41:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63313404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定