简体   繁体   中英

How do i pass the R.id to a method?

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.

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

这些是整数常量,而不是字符串。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM