appwidget中使按钮不能用 以及 appwidget初学指导

本文介绍如何在AppWidget中实现按钮的禁用效果。通过创建两组按钮(启用和禁用状态),并利用RemoteViews控制它们的显示与隐藏,以此达到视觉上的禁用效果。这种方法适用于无法直接设置按钮为禁用状态的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 appwidget中我们可以使用button,但是却不能是button disable 为什么呢

RemoteViews 不能控制一个button可用和不可用的状态,但是可以控制它的显示与隐藏

因为我们可以利用这个造假

<Button android:id="@+id/startbutton" android:text="Start" android:visibility="visible"></Button>
<Button android:id="@+id/startbutton_disabled" android:text="Start" android:clickable="false" android:textColor="#999999" android:visibility="gone"></Button>
 
<Button android:id="@+id/stopbutton" android:text="Stop"  android:visibility="gone"></Button>
<Button android:id="@+id/stopbutton_disabled" android:text="Stop" android:clickable="false" android:textColor="#999999" android:visibility="visible"></Button>

 然后呢

当点击startbutton的时候

RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteView.setViewVisibility(R.id.startbutton, View.GONE);
remoteView.setViewVisibility(R.id.startbutton_disabled, View.VISIBLE);
remoteView.setViewVisibility(R.id.stopbutton, View.VISIBLE);
remoteView.setViewVisibility(R.id.stopbutton_disabled, View.GONE);
AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteView);

 

当点击stop button的时候

RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteView.setViewVisibility(R.id.startbutton, View.VISIBLE);
remoteView.setViewVisibility(R.id.startbutton_disabled, View.GONE);
remoteView.setViewVisibility(R.id.stopbutton, View.GONE);
remoteView.setViewVisibility(R.id.stopbutton_disabled, View.VISIBLE);
AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteView);

 

其实通过一个android:clickable="false" ,还有buuton的隐藏转换 造成了视觉的欺骗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值