2017-06-11 33 views
0

我試圖做一個小部件,只包含一個ImageView的的,當你點擊它,它改變到另一個圖像,隨機從我做了六個圖像中選擇。Android簡約的ImageView的Widget

我的問題是,是,有對如何使小部件沒有什麼好解釋的,所以我不知道如何做到這一點。

我已經做了一個小部件的佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/relativeLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/widget_margin"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     android:adjustViewBounds="true" 
     android:clickable="true" 
     android:contentDescription="@string/widget_description" 
     android:onClick="widgetOnClick" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_bias="0.5" 
     app:srcCompat="@mipmap/ic_widget" /> 
</LinearLayout> 

我試圖讓我的java文件的一些代碼,但沒有做任何事情。

package com.ggblbl.widget; 

import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.RemoteViews; 

import java.util.Random; 

/** 
* Implementation of App Widget functionality. 
*/ 
public class RandomSideWidget extends AppWidgetProvider { 

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, 
           int appWidgetId) { 

     // Construct the RemoteViews object 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.random_side_widget); 

     // Instruct the widget manager to update the widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     // There may be multiple widgets active, so update all of them 
     for (int appWidgetId : appWidgetIds) { 
      updateAppWidget(context, appWidgetManager, appWidgetId); 
     } 
    } 

    @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 widgetOnClick(View v) { 
     ImageView imageView = (ImageView) v.findViewById(R.id.imageView); 
     Random rnd = new Random(); 
     int number = rnd.nextInt(6) + 1; 

     switch(number) { 
      case 1: 
       imageView.setImageResource(R.drawable.widget_blue); 
       break; 
      case 2: 
       imageView.setImageResource(R.drawable.widget_green); 
       break; 
      case 3: 
       imageView.setImageResource(R.drawable.widget_orange); 
       break; 
      case 4: 
       imageView.setImageResource(R.drawable.widget_pink); 
       break; 
      case 5: 
       imageView.setImageResource(R.drawable.widget_red); 
       break; 
      case 6: 
       imageView.setImageResource(R.drawable.widget_yellow); 
       break; 
      default: 
       imageView.setImageResource(R.mipmap.ic_widget); 
       break; 
     } 
    } 
} 

我使用API​​級15

+0

的[在App小部件顯影劑頁](https://developer.android.com/guide/topics/appwidgets/index.html)是相當詳細。你經歷過嗎? –

+0

我真的嘗試過(多次),是的,但我並沒有太多瞭解它。 – ggblbl

回答

0

您應該使用的PendingIntent和IntentService從控件處理點擊。 裏面的updateAppWidget()方法,把代碼如下:

Intent intent = new Intent(context, MyIntentService.class); 
PendingIntent pendingIntent = PendingIntent.getService(context, 1001 + appWidgetId, intent, 0); 
views.setOnClickPendingIntent(R.id.imageView, pendingIntent); 

將您的onclick自定義類的onHandleIntent方法延伸IntentService裏面充塞。

而且還調用相同的方法updateAppWidget()更新作出後。

+0

整個問題是我不知道在那裏和佈局文件中編碼。 – ggblbl

+0

MyIntentService給我一個錯誤? – ggblbl

+0

@ggblbl什麼錯誤? –