我必须使用什么方法代替应用程序小部件中的findViewById? [英] what method I must use Instead of findViewById in app widget?

查看:85
本文介绍了我必须使用什么方法代替应用程序小部件中的findViewById?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 findViewById()时,出现错误.
我应该使用什么方法代替我的应用小部件中的 findViewById()?

When I use findViewById() I get an error.
What method should I use, instead of findViewById() in my app widget?

P.S .:我想做类似的事情:

P.S.: I want to do something like:

 Button a = (Button) findViewById(R.id.button);

在我的屏幕小部件中

推荐答案

您无需在Widget中使用findViewById()即可

You don't need a findViewById() in Widget just do this

创建一个静态变量,它将是您的onClick名称标签:

create a static variable, which will be your onClick name tag:

private static final String MyOnClick = "Button OnClick";

将此onClick标记设置为您的视图,如下所示:

Set this onClick tag to your view as below:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
views.setTextViewText(R.id.your_button, "Button Name");
views.setOnClickPendingIntent(R.id.your_button, MyOnClick);

获取像波纹管这样的按钮单击侦听器

get Button Click listener like bellow

public void onReceive(Context context, Intent intent) {

if (MyOnClick.equals(intent.getAction())){
    //your onClick action is here
  }
};

这里总代码

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class Widget extends AppWidgetProvider {
    private static final String SYNC_CLICKED    = "automaticWidgetSyncButtonClick";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews;
        ComponentName watchWidget;

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        watchWidget = new ComponentName(context, Widget.class);

        remoteViews.setOnClickPendingIntent(R.id.sync_button, getPendingSelfIntent(context, SYNC_CLICKED));
        appWidgetManager.updateAppWidget(watchWidget, remoteViews);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);

        if (SYNC_CLICKED.equals(intent.getAction())) {

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            RemoteViews remoteViews;
            ComponentName watchWidget;

            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            watchWidget = new ComponentName(context, Widget.class);

            remoteViews.setTextViewText(R.id.sync_button, "TESTING");

            appWidgetManager.updateAppWidget(watchWidget, remoteViews);

        }
    }

    protected PendingIntent getPendingSelfIntent(Context context, String action) {
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }
}

这篇关于我必须使用什么方法代替应用程序小部件中的findViewById?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆