java - 找到以编程方式创建的 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);
}
}
解决方案
您可以创建此 TextView 的成员变量,然后您可以在此类中使用它,也可以在 LinearLayout 上使用 findViewById()。
推荐阅读
- android - 在整个应用程序上实现使用选择主题,而不是在每个活动上调用 settheme 或重新创建
- javascript - 在 Chart.js 中显示类别相对于堆积条的百分比
- java - 如何同时停止调用 MotionEvent.ACTION_DOWN & MotionEvent.ACTION_UP & MotionEvent.ACTION_MOVE?
- javascript - 如何使用 fetch 从一个模块导出从 GET API 获得的响应数据到另一个模块
- python - 如何从 json 文件中的 cookie 中仅提取某些值?
- angular - Angular 11 - 如何隔离路线
- c# - 将泛型方法中的枚举值转换为 int
- google-chrome - Chrome + PDF“无法加载插件”
- python - 如何在情节创建后更改子情节标题?
- create-react-app - 在 React-testing-library 中使用 `getByTestId()` 的替代方法