单击时更改小部件可见性

时间:2012-02-24 13:52:38

标签: android android-layout android-intent android-widget

我的小部件包含两个相对布局。我已经使两个布局都可以点击了。以下是布局的ID:

android:id="@+id/upper_layout" 
android:id="@+id/bottom_layout"

现在,我需要的是,如果用户点击upper_layout,则bottom_layout应该是不可见的。

在这里,我到目前为止尝试过但是它没有用。你能检查一下我做错了什么吗? 或者,也许可以建议一些其他方法来实现这一目标。

代码:

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);
    }

}

2 个答案:

答案 0 :(得分:2)

Android 3.0或更多版本中有几个内置的小部件功能。请查看link

答案 1 :(得分:1)

我想你忘记更新小部件了。 你尝试过这样的事吗?

remoteViews.setViewVisibility(R.id.bottom_layout, View.INVISIBLE);
final ComponentName provider = new ComponentName(context, this.getClass());
appWidgetManager.updateAppWidget(provider, views);