【问题标题】:How to programmatically disable onClick handler on Android AppWidget Button如何以编程方式禁用 Android AppWidget 按钮上的 onClick 处理程序
【发布时间】:2010-04-15 17:20:42
【问题描述】:

我在 appwidget 上有一个按钮,我需要以编程方式从服务中“启用”/“禁用”。

第一个想法是调用setBoolean(R.id.buttonid, "setClickable", false) 来禁用它,但显然你不能远程调用setClickable

另一个想法是使用rv.setTextViewText(R.id.buttonid, "") 从中删除文本标签,然后使用rv.setOnClickPendingIntent(R.id.buttonid, null) 删除点击处理程序。不幸的是,将null 传递给它会导致NullPointerException 进入android.app.ActivityThread.handleServiceArgs

还有其他方法可以以编程方式禁用/启用 appwidget 按钮吗?我可以打电话给rv.setViewVisibility(R.id.buttonid, View.GONE) 来完全隐藏按钮而不是禁用它。然而,这将完全破坏整个小部件布局,我想避免它。

我现在使用的解决方案是用setViewVisibility 隐藏按钮并显示其他空白按钮,而不是像以前一样保持appwidget 布局。

【问题讨论】:

    标签: android layout android-widget


    【解决方案1】:

    基本上我遇到了同样的问题,但想完全编程,所以我们只需将布尔值传递给 setBoolean() 函数:

    remoteViews.setBoolean(R.id.imageButton1, "setEnabled", false);
    

    别忘了告诉 Android 更新主屏幕:

        RemoteViews remoteViews = new RemoteViews(context.getResources().getText(R.string.package_name).toString(), R.layout.your_layout);
        ComponentName thisWidget = new ComponentName(context, Widget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(thisWidget, remoteViews);
    

    【讨论】:

      【解决方案2】:

      当您创建 RemoteViews 实例时,您提供了一个布局。如果您想禁用该按钮,请在该按钮上选择带有android:enabled="false" 的布局。

      或者,您可以使用 setOnClickPendingIntent() 并提供一个不会去任何地方的 Intent(例如,广播 Intent 用于无人使用的操作)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-06
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多