You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查找以编程方式创建的TextView并在同类中调用其相关方法?

Hey there! Let's break down how to solve your two questions using your existing code as a starting point.

1. Finding Programmatically Created TextViews

You've got two solid approaches to locate those dynamically made TextViews:

Using findViewById()

Since you're setting a unique ID for each TextView with textView.setId(i) (from 1 to 5), you can directly use findViewById() to grab any specific one. Just pass the ID you assigned:

// For API level < 26, you'll need to cast the result
TextView targetTextView = (TextView) findViewById(3); // Gets the 3rd TextView you created

// For API 26+, casting isn't required
TextView targetTextView = findViewById(3);

⚠️ A quick note: Using hardcoded integers like 1, 2 etc. might risk ID conflicts with system views or other elements in your layout. A safer alternative is to generate a unique ID using View.generateViewId():

textView.setId(View.generateViewId());

If you go this route, store these generated IDs (like in a list) so you can reference them later.

A more reliable method—especially if you plan to work with all the TextViews later—is to save them in a list as you create them. First, add a member variable to your MainActivity:

private List<TextView> textViewList = new ArrayList<>();

Then update your CreateDesign() method to add each new TextView to the list:

public void CreateDesign(){ 
    linearLayout = findViewById(R.id.linearLayout); // No need to cast in API 26+
    for(int i = 1; i <= 5; i++){ 
        TextView textView = new TextView(this); // Use a local variable instead of the member one
        textView.setId(i); 
        textView.setText(i + ". TextView"); 
        linearLayout.addView(textView);
        textViewList.add(textView); // Add to the list
    } 
}

Now you can easily access any TextView by index:

TextView firstTextView = textViewList.get(0); // Gets the first TextView you created
2. Calling Methods on These TextViews

Once you've located the TextViews, calling methods like setText() or setBackgroundColor() is straightforward:

If you used findViewById()

Just make sure the TextView isn't null before calling methods (to avoid crashes):

TextView targetTv = findViewById(2);
if(targetTv != null) {
    targetTv.setText("Updated Text for 2nd TextView");
    targetTv.setBackgroundColor(Color.BLUE);
}

If you used a Collection

You can either update a specific TextView by index or loop through all of them:

// Update a single TextView
textViewList.get(4).setText("Last TextView Updated!");
textViewList.get(4).setBackgroundColor(Color.GREEN);

// Update all TextViews
for(TextView tv : textViewList) {
    tv.setText("All TextViews Updated");
    tv.setBackgroundColor(Color.LIGHT_GRAY);
}

Also, a quick fix for your existing code: Your current member variable private TextView textView; gets overwritten in each loop iteration, so it only holds the last TextView you created. Using a list as we did above lets you keep track of all of them.

内容的提问来源于stack exchange,提问作者FatalError

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:26:17