窗口小部件双击 [英] Widget double click

查看:102
本文介绍了窗口小部件双击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小部件(AppWidgetProvider),我想知道是否有一种方法可以支持多种clicks.Example:

I have a widget (AppWidgetProvider) and i want to know if there is a way to support multiple clicks.Example:

1)如果是第一次点击的小窗口,的小窗口的变化则的ImageButton(例如,改变颜色)。

1)If is the first click on the widget, then the ImageButton of the widget changes (for example, changes the color).

2)如果是第二次,然后打开一个活动。

2)If is the second time, then open an Activity.

- 有一些方法来处理点击里面AppWidgetProvider事件

-- There is some way to handle click events inside AppWidgetProvider?

我的code:

public class MyWidgetProvider extends AppWidgetProvider
{


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];    

            Intent intent = new Intent(context, MyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);


            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.control_widget);

            views.setOnClickPendingIntent(R.id.asdf, pendingIntent);

             appWidgetManager.updateAppWidget(appWidgetId, views);
        }    
    }
}

我的小部件工作正常。当我点击的ImageButton(R.id.asdf),它进入到活动MyActivity。

My widget is working fine. When i click the ImageButton(R.id.asdf), it goes to the activity MyActivity.

我想知道我该如何处理点击我的窗口小部件事件做出不同的动作(例如:改变的ImageButton的颜色),而不是去一些活动。有一些其他的方法来一些点击手柄除了setOnClickPendingIntent()?

Id like to know how can i handle click events on my widget to make a different action (example: change the color of the ImageButton) instead of go to some activity. Is there some other way to some click handle besides setOnClickPendingIntent()?

推荐答案

我就是这么做的:

1)如果是第一次点击Widget时,小部件的变化那么的ImageButton

1)If is the first click on the widget, then the ImageButton of the widget changes

2)如果是第二次,然后打开一个活动,并返回到inicial ImageButton的状态。

2)If is the second time, then open an Activity and return to the inicial ImageButton state.

林处理click事件与setOnClickPendingIntent:

Im handling click events with setOnClickPendingIntent:

private int[] RESOURCES = {R.drawable.button1,R.drawable.button2};

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


            ComponentName me = new ComponentName(context, MyWidgetProvider.class);
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.my_widget);

            Intent widgetIntent = new Intent(context, MyWidgetProvider.class);
            Intent myIntent= new Intent(context, MyOtherActivity.class); 

            widgetIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            widgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

            PendingIntent pendingIntent;

            if(clicks == 0)
            {
                clicks = 1;
                remoteViews.setImageViewResource(R.id.my_image_button, RESOURCES[0]);
                pendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            }
            else if(clicks == 1)
            {
                clicks = 2;
                remoteViews.setImageViewResource(R.id.my_image_button, RESOURCES[1]);
                pendingIntent = PendingIntent.getActivity(context, 0, myIntent,0);
            }
            else //clicks == 2
            {
                clicks = 0;
                remoteViews.setImageViewResource(R.id.my_image_button, RESOURCES[0]);
                pendingIntent = PendingIntent.getBroadcast(context, 0, widgetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            }

            remoteViews.setOnClickPendingIntent(R.id.my_image_button, pendingIntent);

            mgr.updateAppWidget(me, remoteViews);
        }

        @Override
        public void onEnabled(Context context) {
            clicks = 0;     
            super.onEnabled(context);

        }

这篇关于窗口小部件双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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