findViewById 未定义类型<我的类名在这里>在 AppWidgetProvider 中我的类名在这里>
是否有等效的代码行可以用于 findViewById ? Eclipse 告诉我 AppWidgetProvider 中的类型未定义。
我正在尝试为按钮设置一个点击处理程序。
public class WidgetProvider extends AppWidgetProvider implements OnClickListener{
final Button buttonOnOff = (Button) findViewById(R.id.button_on_off);
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
/*
* Set Click Listener
*/
buttonOnOff.setOnClickListener(this);
/*
* The following 3 lines of code are required to display the widget on the screen.
*/
RemoteViews remoteView = new RemoteViews(context.getPackageName(),
R.layout.appwidget_layout);
ComponentName myWidget = new ComponentName(context,
WidgetProvider.class);
appWidgetManager.updateAppWidget(myWidget, remoteView);
}
@Override
public void onClick(View v) {
}
}
Is there an equivalent line of code I can use for findViewById ? Eclipse tells me it's undefined for the type in an AppWidgetProvider.
I'm trying to set up a click handler for the button.
public class WidgetProvider extends AppWidgetProvider implements OnClickListener{
final Button buttonOnOff = (Button) findViewById(R.id.button_on_off);
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
/*
* Set Click Listener
*/
buttonOnOff.setOnClickListener(this);
/*
* The following 3 lines of code are required to display the widget on the screen.
*/
RemoteViews remoteView = new RemoteViews(context.getPackageName(),
R.layout.appwidget_layout);
ComponentName myWidget = new ComponentName(context,
WidgetProvider.class);
appWidgetManager.updateAppWidget(myWidget, remoteView);
}
@Override
public void onClick(View v) {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用 AppWidgets 时,代码的工作方式相当不同。本质上,您需要使用 RemoteView 而不是传统的 Button 和 findViewByIds。有关如何编写 AppWidget 的链接,请参阅此相关答案。特别是,developer.com 教程的两个链接。
The code works rather differently when you're using AppWidgets. Essentially you need to be working with RemoteView rather than traditional Buttons and findViewByIds. See this related answer for links on how to write AppWidgets. In particular, the two links for developer.com tutorials.