我如何知道或捕捉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时,意图是ShowActivity.class。但我真正想要的是当点击ImageView时,我想显示“Toast”消息而不进行任何活动。
答案 0 :(得分: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 );
答案 1 :(得分: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();
}
});