2011-03-18 67 views
22

我已經創建了安卓窗口小部件,它成功的工作,但現在我想用點擊事件的小部件,以便我可以打開新的活動。安卓窗口小部件點擊事件

幫助我

+1

當你找到任何答案正確,接受它,但是把蜱標記。 – Mathew 2011-03-18 04:30:34

+1

查看http://stackoverflow.com/questions/2748590/clickable-widgets-in-android – 2014-09-07 17:52:01

回答

-3

內部佈局,對於特定的部件給

android:onClick="your method name inside your activity" 

,並在您的活動,請提供:

public void methodname(View view) { 
     //give your intent code here 
} 

注意:當你調用一個方法就是這樣,你的方法應該是公共的,它應該有一個View對象作爲參數。

+1

android:onClick僅適用於Android 1.6+ – Raunak 2011-03-18 05:04:41

+0

您正在使用哪個android版本? – Mathew 2011-03-18 06:14:49

+1

android:onClick在小部件中不受支持。如果你有這個,小部件甚至不會出現在主屏幕上 - 它會顯示錯誤消息無法加載小部件... – coolcool1994 2014-08-26 07:35:02

8

我用這個:

// Create an Intent to launch ExampleActivity 
     Intent intent = new Intent(context, Mainpage.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
     remoteViews.setOnClickPendingIntent(R.id.widget, pendingIntent); 
+10

是的,但是當點擊小部件時將調用的方法在哪裏? – KalEl 2012-01-04 13:46:01

+0

謝謝...它幫助 – 2012-09-14 06:16:12

35
@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
        int[] appWidgetIds) { 
    for (int i = 0; i < appWidgetIds.length; i++) { 
     int appWidgetId = appWidgetIds[i]; 

     Intent intent = new Intent(context, TaskManagerActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); 
     views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent); 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 
} 

在widget.xml我有根元素LinearLayout中與ID widget_layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/widget_layout" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:paddingTop="10dip" 
      android:paddingLeft="10dip" 
      android:orientation="vertical"> 
+1

這爲我工作,謝謝你:) – 2012-11-21 21:09:07

+0

嗨@xtem,我面臨檢測部件ID問題。我在主屏幕上創建了3個小部件,並且我想分別更改每個人的主題。所以當我點擊小部件時,我的Configuration類被打開,onUpdate()每個小部件都被更新。我可以單獨做嗎? – 2012-12-07 08:55:57