0

我如何知道或捕获 ImageView onclicklistener?

public class AppWidget extends AppWidgetProvider{
    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, ShowActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            views.setOnClickPendingIntent(R.id.imageView1, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

在上面的编码中,当点击 ImageView 时,intent 到 ShowActivity.class。但我真正想要的是当单击 ImageView 时,我想显示“Toast”消息而不进行任何活动。

4

2 回答 2

1

它会显示你想要什么:

Intent intent = new Intent(context, ShowActivity.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);          
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendIntent = PendingIntent.getActivity(context, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.image1, pendIntent);
appWidgetManager.updateAppWidget(appWidgetId, views );
于 2013-04-25T04:21:58.320 回答
0

你试过onClickListener吗?

 ImageView img = (ImageView) findViewById(R.id.img_id);
 img.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
    Toast.makeText(YourActivityName.this,
            "My Toast Text", Toast.LENGTH_LONG).show();
    }
 });
于 2013-04-25T02:53:14.030 回答