【发布时间】:2010-07-20 04:50:31
【问题描述】:
我想要一个“Utils”类,它将在我的代码中使用多种方法。例如,我有一个带有textview 和两个ImageButtons 的顶栏,它们必须在不同的活动上显示不同的文本和图标。
我发现自己在每个活动中都写了这样的东西:
(TextView) topBarText = (TextView) findViewById(R.id.topBarText);
topBarText.setText(R.id.mytextForThisView);
我想在我的整个应用程序中一次 findViewById,并调用一个方法setupTopBar(String text, R.id.iconForImageButton1, R.id.iconForImageButton2),甚至传递当前 Activity 的 id 并让该方法找出在文本和图像中显示的内容。
我创建了 Util 类,但它没有扩展 Activity。问题是,如果没有,findViewById 就无法访问,所以我不能使用它。
在 Android 中执行此类操作的模式是什么?
【问题讨论】:
标签: android class android-activity extends