[英]Is there a way to change all TextViews in Android?
我試圖讓我的 textView 不可見 4 秒,然后在使用代碼后出現:
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);
在我的活動中有超過 100 個文本視圖並且我希望所有文本視圖都這樣做的情況下,除了為所有 100 個文本視圖編寫代碼之外,有沒有辦法將其應用於所有文本視圖? 任何幫助將不勝感激,謝謝!
將所有這些 TextView 添加到一些 LinearLayout 中,並在給定內部之后隱藏 LinearLayout 布局,而不是單獨隱藏每個 TextView。
您可以使用自定義 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<TextView> textViewList;
public AnimTextView(TextView ... textViews) {
textViewList = new ArrayList<>();
textViewList.addAll(Arrays.asList(textViews));
}
public void startLoading(){
for (final TextView textView: textViewList) {
textView.setText("Loading data...");
textView.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
textView.setVisibility(View.INVISIBLE);
}
}, 4000);
}
}
}
現在,在您的主要活動中:
AnimTextView animTextView = new AnimTextView(textView1, textView2, textView3, textView4, ...);
當您需要開始加載時:
animTextView.startLoading();
這是 function ,它將獲取 Activity 的所有 TextViews(甚至是嵌套的)並隱藏,您可以從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是您的 Root 視圖
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.