1

我有2種可能的Widget佈局,用戶可以通過偏好活動來改變,我的意思是光明和黑暗。兩者看起來都是一樣的,onlu的顏色是不同的。事件ID是相同的。我想給點擊整個小部件的可能性,什麼會打開活動並通過意向發送necessery數據。我在AppWidgetProvider中的代碼根本不起作用。Android - Widget點擊監聽器不起作用

這就是:

private final String ACTION = "CLICK_MY_BUTTON"; 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    super.onUpdate(context, appWidgetManager, appWidgetIds); 
    SharedPreferences theme = Data.getPreferences(context, Data.Prefs.THEME); 
    boolean dark = theme.getString(Data.widgetsThemeSetings, "1").equals("1"); 
    int layoutId = dark?R.layout.widget_dark:R.layout.widget_light; 
    for (int i:appWidgetIds) { 
     Intent intent = new Intent(context, MainActivity.class); 
     intent.putExtra("from", "widget"); 
     intent.setAction(ACTION); 
     int id = Calendar.getInstance().get(Calendar.DAY_OF_YEAR); 
     intent.putExtra("id", id<60?id:id-1); 
     intent.putExtra("widgetID", i); 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), layoutId); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, 0); 
     remoteViews.setOnClickPendingIntent(R.id.widget_relative_main, pendingIntent); 
     String output = ""; 
     for (String s:HolidayCalendar.getInstance(context).getTodayTexts()) { 
      output += "\n\n" + Data.pointer + " " + s; 
     } 
     remoteViews.setTextViewText(R.id.widget_text_holiday, output.substring(2)); 
     appWidgetManager.updateAppWidget(i, remoteViews); 
    } 
} 

和我的小部件佈局之一:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/widget_relative_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/rounded_widget_dark" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/widget_text_today" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:gravity="center" 
     android:text="@string/today_is" 
     android:textColor="#FFF" 
     android:textSize="20sp" /> 

    <TextView 
     android:id="@+id/widget_text_holiday" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/widget_text_today" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:textColor="#FFF" /> 

</RelativeLayout> 

如何得到它的工作?我不知道什麼可能是不正確的...

+0

你想要textview點擊事件嗎? – Rasel

+0

@Rasel我想單擊整個小部件 – Andret

回答

2

這是我的工作代碼。這將工作。

for (int i = 0; i < N; i++) { 
      int awID = ids[i]; 

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

       Intent intent = new Intent(this, your intented class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

       //views.setTextViewText(R.id.widgetcount, visitstext); 
       PendingIntent pendingIntent = PendingIntent.getActivity(this, 
         0, intent, 0); 

       views.setOnClickPendingIntent(R.id.widgetimage, pendingIntent); 
manager.updateAppWidget(awID, views); 
    } 
+0

這對我有用,謝謝! – Andret