我试图制作一个由一个imageView组成的小部件,当你点击它时,它会改变为另一个图像,当我在Web按钮上选项卡时,它会打开浏览器并打开一个站点。
我的问题是,关于如何制作小部件没有好的教程,所以我不知道如何做到这一点。
我已经制作了一个小部件布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09C"
android:padding="@dimen/widget_margin">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:onClick="Web"
android:text="Web"
tools:layout_editor_absoluteX="110dp"
tools:layout_editor_absoluteY="6dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/button"
android:onClick="onClick"
android:text="Image" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/button2"
android:onClick="Music"
android:text="Music" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/button3"
android:adjustViewBounds="false"
android:src="@drawable/fox"
android:onClick="widgetOnClick"
android:clickable="true"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
app:srcCompat="@drawable/fox2"
/>
我已经尝试在我的.java文件中创建一些代码,但是当我注释掉有关imageView的所有内容时,只能打开Web
void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.wykop.pl"));
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views1 = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views1.setOnClickPendingIntent(R.id.button, pending);
appWidgetManager.updateAppWidget(appWidgetId, views1);
intent = new Intent(Intent.ACTION_VIEW);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
RemoteViews views2 = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views2.setOnClickPendingIntent(R.id.button2, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views2);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
public void onReceive(final Context context, Intent intent) {
super.onReceive(context, intent);
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews views1 = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
final RemoteViews views2 = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
if (intent.getAction().equals(intent)) {
appWidgetManager.updateAppWidget(new ComponentName(context, NewAppWidget.class), views1);
}
if (intent.getAction().equals(intent)) {
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
imageView2.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
appWidgetManager.updateAppWidget(new ComponentName(context, NewAppWidget.class), views2);
}
});
imageView2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
imageView.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
appWidgetManager.updateAppWidget(new ComponentName(context, NewAppWidget.class), views2);
}
});
}
}
}