首页 > 解决方案 > 找到以编程方式创建的 TextView?

问题描述

如何在同一类中找到那些 TextView 的另一个函数?我将在创建后使用setText(), serBackgroundColor()

此代码部分已打开CreateDesign(),并且此功能calling onCreate()

public class MainActivity extends AppCompatActivity {

private LinearLayout linearLayout;
private TextView textView;

public void CreateDesign(){

   linearLayout = (LinearLayout) findById(R.id.linearLayout);

   for(int i = 1; i <= 5; i++){
        textView = new TextView(this);
        textView.setId(i);
        textView.setText(i + ". TextView");
        
        linearLayout.addView(textView);
    }
}

标签: javaandroidprogrammatically

解决方案


您可以创建此 TextView 的成员变量,然后您可以在此类中使用它,也可以在 LinearLayout 上使用 findViewById()。


推荐阅读