如何将 R.id 传递给方法?
我有这个类:
public class FlowerForYouWidget extends AppWidgetProvider
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
....
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.flower_widget);
buildClock(context, views, "R.id.widgetHour", curHour);
....
}
public static RemoteViews buildClock(Context context, RemoteViews views, String btnId, String currentValue)
{
....
views.setImageViewBitmap(btnId, myBitmap);
....
}
我需要将“R.id.widgetHour”传递给 buildClock() 方法并将 setImageViewBitmap 作为参数。
I have this class:
public class FlowerForYouWidget extends AppWidgetProvider
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
....
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.flower_widget);
buildClock(context, views, "R.id.widgetHour", curHour);
....
}
public static RemoteViews buildClock(Context context, RemoteViews views, String btnId, String currentValue)
{
....
views.setImageViewBitmap(btnId, myBitmap);
....
}
I need to pass the "R.id.widgetHour" to buildClock() method and to setImageViewBitmap as param.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
R 类维护的所有引用都是 int 的。只需将其作为 int 传递,您就可以在函数中使用它
All references maintained by the R class are int's. Just pass it through as an int and you'll be able to use it within your function
这些是整数常量,而不是字符串。
Those are integer constants, not strings.
R 类常量是整数,是应用程序资源中组件的唯一 ID。
所以将其作为整数传递,然后就可以正常使用了。
OR 因为您当前拥有它(字符串),您可以使用
getResources().getIdentifier()
通过名称获取此 id。但你甚至不需要通过它; R 类是完全静态的,因此您可以从任何地方访问您的所有 id。
R class constants are integers, unique id's for components in your app's resources.
So pass it as an integer, then you can use it normally.
OR as you have it currently (a string) you can use
getResources().getIdentifier()
to get this id by the name.But you don't even need to pass it; R class is fully static so you can access all of your id's from everywhere.