如何以编程方式禁用的Andr​​oid AppWidget按钮的onClick处理程序 [英] How to programmatically disable onClick handler on Android AppWidget Button

查看:96
本文介绍了如何以编程方式禁用的Andr​​oid AppWidget按钮的onClick处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有appwidget一个按钮,我需要从服务到启用/禁用编程。

I have a Button on appwidget, that I need to 'enable'/'disable' programmatically from a Service.

最初的想法是叫 setBoolean(R.id.buttonid,setClickable,FALSE)来禁用它,但显然你不能叫 setClickable 远程

First idea was to call setBoolean(R.id.buttonid, "setClickable", false) to disable it, but apparently you can't call setClickable remotely.

另一个想法是来自它 rv.setTextViewText删除文本标签(R.id.buttonid,),然后通过<$ c取出单击处理程序$ C> rv.setOnClickPendingIntent(R.id.buttonid,空)。不幸的是通过来它会导致 NullPointerException异常 android.app.ActivityThread.handleServiceArgs

Another idea was was remove the text label from it with rv.setTextViewText(R.id.buttonid, "") and then remove the click handler by rv.setOnClickPendingIntent(R.id.buttonid, null). Unfortunately passing null to it causes NullPointerException in in android.app.ActivityThread.handleServiceArgs

有一些其他的方式来编程禁用/启用appwidget按钮?我可以叫 rv.setViewVisibility(R.id.buttonid,View.GONE)来隐藏按钮完全不是禁用它。这将然而,彻底打破整个部件的布局,我想避免它。

Is there some other way to programmatically disable/enable appwidget Button? I could just call rv.setViewVisibility(R.id.buttonid, View.GONE) to hide the button completely instead of disabling it. This would however completely break whole widget layout and I would like to avoid it.

我现在使用的溶液与 setViewVisibility 隐藏按钮,而是显示其他空白按钮保持appwidget布局,因为它以前。

The solution I'm using now is hiding the button with setViewVisibility and showing other blank button instead to the keep appwidget layout as it was before.

推荐答案

当您创建 RemoteViews 例如,你提供一个布局。当您希望禁用的按钮,选择与 Android的布局:启用=FALSE该按钮

When you create your RemoteViews instance, you supply a layout. When you want to disable the button, choose a layout with android:enabled="false" on that button.

或者,你可以使用 setOnClickPendingIntent()并提供一个意图,就不会去任何地方(例如,广播意图为无人使用的动作)。

Or, you could use setOnClickPendingIntent() and supply an Intent that just will not go anywhere (e.g., a broadcast Intent for an action that nobody uses).

这篇关于如何以编程方式禁用的Andr​​oid AppWidget按钮的onClick处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆