【问题标题】:Can't disable android.widget.Button无法禁用 android.widget.Button
【发布时间】:2016-07-21 23:42:30
【问题描述】:

我在 Android 小部件中有一个按钮,声明如下:

<Button android:id="@+id/newWorkBtnWidget"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:text="@string/ts_on_repair"/> 

每次我尝试通过 RemoteViews 启用或禁用此按钮时,我都会收到错误 android.widget.RemoteViews$ActionException: view: android.widget.Button can't use method with RemoteViews: setEnabled(boolean)

我的代码:

RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                        R.layout.widget);

remoteViews.setInt(R.id.newWorkBtnWidget, "setBackgroundResource",
    R.drawable.green_button);
remoteViews.setBoolean(R.id.newWorkBtnWidget, 
   "setEnabled", false);

如何解决?

【问题讨论】:

  • 呃,这是什么,newWorkBtnWidgetnewWorkButtWidget
  • 抱歉,这应该是同一个按钮,已修复。无论如何,这与问题无关。

标签: android


【解决方案1】:

我最近遇到了同样的问题,我发现方法名为“setEnabled”的 remoteView.setBoolean() 也不起作用。

使用

remoteViews.setBoolean(R.id.textView_life_id, "setEnabled", true);

会出现这样的错误

W/AppWidgetHostView: updateAppWidget couldn't find any view, using error view
                                              android.widget.RemoteViews$ActionException: view: android.widget.TextView can't use method with RemoteViews: setEnabled(boolean)
                                                  at android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1134)
                                                  at android.widget.RemoteViews.performApply(RemoteViews.java:2304)
                                                  at android.widget.RemoteViews.apply(RemoteViews.java:2263)
                                                  at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:402)
                                                  at com.android.launcher2.LauncherAppWidgetHostView.updateAppWidget(LauncherAppWidgetHostView.java:54)
                                                  at android.appwidget.AppWidgetHost.updateAppWidgetView(AppWidgetHost.java:434)
                                                  at android.appwidget.AppWidgetHost$UpdateHandler.handleMessage(AppWidgetHost.java:102)
                                                  at android.os.Handler.dispatchMessage(Handler.java:99)
                                                  at android.os.Looper.loop(Looper.java:138)
                                                  at android.app.ActivityThread.main(ActivityThread.java:5089)
                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                  at java.lang.reflect.Method.invoke(Method.java:511)
                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                  at dalvik.system.NativeStart.main(Native Method)

最后我找到了答案,只有SDK中带有@android.view.RemotableViewMethod的方法才能在remoteview中使用。

喜欢

@android.view.RemotableViewMethod
public final void setText(CharSequence text) {
     setText(text, mBufferType);
}

那么你可以使用views.setCharSequence(R.id.textView, "setText", "Test");

【讨论】:

    【解决方案2】:

    您不能使用传统意义上的启用/禁用,但您可以更新按钮以使其具有禁用的外观,或者具有“假”按钮,然后使用 GONE/VISIBLE 将它们切换出来。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多