1

嗨首先我很抱歉我的英语不好。如何设置 RemoteViews .setViewVisibility?当我单击它时,我想隐藏小部件按钮。这是我的代码。感谢帮助。

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++){

        int appId = appWidgetIds[i];
      widget = new RemoteViews(context.getPackageName(), R.layout.widget_wyglad);
      Intent Tv = new Intent(context, client_widget.class);
      Tv.setAction(AKCJA);
      Tv.putExtra("test", AKCJA);
      PendingIntent ptv = PendingIntent.getBroadcast(context, 0, Tv, 0);
      widget.setOnClickPendingIntent(R.id.bt_wid_tv, ptv);
      appWidgetManager.updateAppWidget(appId, widget);
    }

  super.onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onReceive(Context context, Intent intent) {

    Bundle extras = intent.getExtras();
    if (extras != null){

        widget.setViewVisibility(R.id.bt_wid_tv, View.GONE);

    }
    else {Log.d("ERR", "EXTRAS ELSE");}

    super.onReceive(context, intent);   

}

}
4

3 回答 3

3

您已经完成了隐藏视图的所有工作。只需将其包含在您的代码中-

在您的 onReceive 方法中-

if (intent.getAction().equals(AKCJA)) {
widget.setViewVisibility(R.id.bt_wid_tv, View.GONE);
}
于 2013-07-20T18:43:51.907 回答
-1

您是否尝试过使用INVISIBLE而不是GONE?Gone 将删除视图,就好像它从未存在过一样。并且不可见将保持视图在布局中的位置,但使其不可见。

rv.setViewVisibility(R.id.bt_wid_tv, View. INVISIBLE);
于 2013-07-20T18:43:55.890 回答
-2

是的,它终于起作用了,这就是我要改变的:

int appId = appWidgetIds[i]; 

改成:

appId = appWidgetIds[i];

接下来我添加静态 int appId:

public class client_widget extends AppWidgetProvider {

static int appId;

最后添加这个新的 AppWidgetManager 和更新行:

public void onReceive(Context context, Intent intent) {

AppWidgetManager newAppWidgetManager = AppWidgetManager.getInstance(context);

        if (intent.getAction().equals(AKCJA)) {
            AppWidgetManager nowy = AppWidgetManager.getInstance(context);

            widget.setViewVisibility(R.id.bt_wid_tv, View.GONE);

            newAppWidgetManager.updateAppWidget(appId, widget);

谢谢@Naddy

于 2013-07-21T08:49:58.670 回答