setOnClickPendingIntent

setOnClickPendingIntent

appwidget获取输入的最佳方法是什么。我知道我可以使用setOnClickPendingIntent中的RemoteViews方法,但是我不想要这样!还有其他方法吗?
附言当我尝试在Xml(onClick)中注册<ImageView android:onClick="myHandler" ... />侦听器时,将引发异常。

W/AppWidgetHostView(  178): android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.Button
W/AppWidgetHostView(  178):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
W/AppWidgetHostView(  178):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
W/AppWidgetHostView(  178):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
W/AppWidgetHostView(  178):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
W/AppWidgetHostView(  178):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
W/AppWidgetHostView(  178):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
W/AppWidgetHostView(  178):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
W/AppWidgetHostView(  178):     at android.widget.RemoteViews.apply(RemoteViews.java:900)
W/AppWidgetHostView(  178):     at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:208)
W/AppWidgetHostView(  178):     at com.htc.launcher.LauncherAppWidgetHostView.updateAppWidget(LauncherAppWidgetHostView.java:116)
W/AppWidgetHostView(  178):     at android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:243)
W/AppWidgetHostView(  178):     at android.appwidget.AppWidgetHost$UpdateHandler.handleMessage(AppWidgetHost.java:72)
W/AppWidgetHostView(  178):     at android.os.Handler.dispatchMessage(Handler.java:99)
W/AppWidgetHostView(  178):     at android.os.Looper.loop(Looper.java:123)
W/AppWidgetHostView(  178):     at android.app.ActivityThread.main(ActivityThread.java:4595)
W/AppWidgetHostView(  178):     at java.lang.reflect.Method.invokeNative(Native Method)
W/AppWidgetHostView(  178):     at java.lang.reflect.Method.invoke(Method.java:521)
W/AppWidgetHostView(  178):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
W/AppWidgetHostView(  178):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
W/AppWidgetHostView(  178):     at dalvik.system.NativeStart.main(Native Method)
W/AppWidgetHostView(  178): Caused by: java.lang.reflect.InvocationTargetException
W/AppWidgetHostView(  178):     at android.widget.Button.<init>(Button.java:65)
W/AppWidgetHostView(  178):     at java.lang.reflect.Constructor.constructNative(Native Method)
W/AppWidgetHostView(  178):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
W/AppWidgetHostView(  178):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
W/AppWidgetHostView(  178):     ... 19 more
W/AppWidgetHostView(  178): Caused by: java.lang.IllegalStateException: The android:onClick attribute cannot be used within a restricted context
W/AppWidgetHostView(  178):     at android.view.View.<init>(View.java:2005)
W/AppWidgetHostView(  178):     at android.widget.TextView.<init>(TextView.java:335)
W/AppWidgetHostView(  178):     at android.widget.Button.<init>(Button.java:69)
W/AppWidgetHostView(  178):     ... 23 more

最佳答案

我知道我可以使用RemoteViews中的setOnClickPendingIntent方法,但我不希望这样做!


您别无选择。这是API级别10(姜饼)的唯一输入选项。

对于API级别11和更高版本(蜂窝等),您可以创建包含ListViewGridViewStackView的应用程序小部件。对于这些,您可以提供另一个PendingIntent,当单击列表(或网格或堆栈)中的项目时将调用该setOnClickPendingIntent()。这将与类似。

10-07 18:42