Make a toggle button to widget

2019-09-01 00:19发布

问题:

How do I add a button to a widget, not for opening an activity or something?

Just for toggling for example Wi-Fi :) Could it be done by a ImageButton or something like that? I just can't figure out how to add an onClickListener.

回答1:

I have updated my answer now that I understand you are trying to write an AppWidget.

AppWidgets support only a limited set of Views and ToggleButton is not one of them. The list of Views supported by AppWidgets can be found here:

http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout

You will have to use an ImageButton and remember its toggled state yourself. You can set it's image source to a different image depending on whether it is toggled or not. See ImageButton for more info:

http://developer.android.com/reference/android/widget/ImageButton.html