Change widget visibility on click
My widget consists two relative layouts. I have made both the layouts clickable. Following are the id's of the layout:
android:id="@+id/upper_layout"
android:id="@+id/bottom_layout"
Now, what I need is that if a user clicks on the upper_layout, bottom_layout should be invisible.
Here, is what I have tried so far but it's not working. Can u check what I am doing wrong? Or, maybe suggest some other ways to achieve this.
Code :
public class BobsWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER = "Clicked";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
Intent active = new Intent(context, BobsWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.upper_layout,
actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// check, if our Action was called
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.main);
remoteViews.setViewVisibility(R.id.bottom_layout, View.INVISIBLE);
}
super.onReceive(context, intent);
}
}
您可以在Anndroid 3.0或更多版本中使用几种内置小工具功能。请检查此链接
I think you forgot to update the widget. Have you tried something like this ?
remoteViews.setViewVisibility(R.id.bottom_layout, View.INVISIBLE);
final ComponentName provider = new ComponentName(context, this.getClass());
appWidgetManager.updateAppWidget(provider, views);
链接地址: http://www.djcxy.com/p/10506.html
上一篇: 如何在本地IIS上创建别名路径?
下一篇: 点击时更改小部件可见性