如何在 Android 主屏幕小部件中隐藏/显示按钮

发布于 2024-12-06 01:34:31 字数 254 浏览 1 评论 0原文

我是android开发的初学者。目前,我正在创建一个小型主屏幕小部件,单击按钮即可更改手机壁纸。设置壁纸工作正常,但我想制作一个可点击的小图片(ImageView)以允许用户显示和隐藏此设置按钮。

我在服务上设置它并使用 PendingIntent 以便将我的 onClick 事件附加到同一服务,但我无法检测按钮的属性是显示还是隐藏。

因此,是否有任何建议和解决方案可以使我的 ImageView 显示或隐藏主屏幕小部件中的按钮?

提前致谢..

I am a beginner of android development. Current, I am working on creating a small home screen widget that is changing wallpaper of the mobile on click the button. The setting wallpaper is working fine but I want to make a clickable small picture (ImageView) to allow user to show and hide this setting button.

I setup it on service and use PendingIntent in order to attach my onClick event to the same service, but I cannot detect the property of button whether showing or hiding.

Therefore,is there any suggestion and solution to make my ImageView to show or hide the button in home screen widget?

Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别挽留 2024-12-13 01:34:31

您可以使用 mButton.setVisibility(View.GONE) 隐藏按钮。

您还可以使用 mButton.isShown() 在布尔变量中检查按钮的可见性状态。

编辑:

例如

AppWidgetProvideronReceive()中,

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

     remoteViews.setViewVisibility(viewId, visibility);

因此为了隐藏按钮

     remoteViews.setViewVisibility(R.id.buttonId,View.INVISIBLE);

Edit - 2:根据Kartik的评论,

示例代码:

    public class ButtonHideShowWidget extends AppWidgetProvider {

     private static boolean status = false;

     @Override
     public void onReceive(Context context, Intent intent) {
      super.onReceive(context, intent);
      if (intent.getAction()==null) {

             Bundle extras = intent.getExtras();
             if(extras!=null) {

                 remoteViews = new RemoteViews( context.getPackageName(), R.layout.your_widget_layout );
                 if(status){
                   remoteViews.setViewVisibility(R.id.buttonId,View.INVISIBLE);
                  status = false;
                 }else{
                   remoteViews.setViewVisibility(R.id.buttonId,View.VISIBLE);
                  status = true;
              }

                 watchWidget = new ComponentName( context, ButtonHideShowWidget.class );

                 (AppWidgetManager.getInstance(context)).updateAppWidget( watchWidget, remoteViews );
                 //Toast.makeText(context, "Clicked "+status, 2000).show();
             }
         }
    }
}

You can use mButton.setVisibility(View.GONE) to hide the button.

You can also check state of button's visibility in a boolean variable using mButton.isShown().

Edited:

For Example

In onReceive() of AppWidgetProvider,

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

     remoteViews.setViewVisibility(viewId, visibility);

So for hiding your Button

     remoteViews.setViewVisibility(R.id.buttonId,View.INVISIBLE);

Edit - 2: According to Kartik's comment,

Sample Code:

    public class ButtonHideShowWidget extends AppWidgetProvider {

     private static boolean status = false;

     @Override
     public void onReceive(Context context, Intent intent) {
      super.onReceive(context, intent);
      if (intent.getAction()==null) {

             Bundle extras = intent.getExtras();
             if(extras!=null) {

                 remoteViews = new RemoteViews( context.getPackageName(), R.layout.your_widget_layout );
                 if(status){
                   remoteViews.setViewVisibility(R.id.buttonId,View.INVISIBLE);
                  status = false;
                 }else{
                   remoteViews.setViewVisibility(R.id.buttonId,View.VISIBLE);
                  status = true;
              }

                 watchWidget = new ComponentName( context, ButtonHideShowWidget.class );

                 (AppWidgetManager.getInstance(context)).updateAppWidget( watchWidget, remoteViews );
                 //Toast.makeText(context, "Clicked "+status, 2000).show();
             }
         }
    }
}
裸钻 2024-12-13 01:34:31

调用 setVisibility(View.Invisible);借助用户单击按钮后您创建的按钮对象。

Call setVisibility(View.Invisible); with the help of button object created by you after the user clicks the button.

神回复 2024-12-13 01:34:31
// To remove button
Button button = (Button) findViewById(R.id.button);
button.setVisibility(View.GONE);

// To transparent button
Button button = (Button) findViewById(R.id.button);
button.setVisibility(View.INVISIBLE);
// To remove button
Button button = (Button) findViewById(R.id.button);
button.setVisibility(View.GONE);

// To transparent button
Button button = (Button) findViewById(R.id.button);
button.setVisibility(View.INVISIBLE);
一梦浮鱼 2024-12-13 01:34:31

您不应该在 onReceive(Context, Intent) 方法中执行此操作,如官方文档中所述

每次广播时都会在上述每个回调方法之前调用此方法。您通常不需要实现此方法,因为默认的 AppWidgetProvider 实现会过滤所有 App Widget 广播并根据需要调用上述方法。

您应该在 onAppWidgetOptionsChanged() 中执行此操作。
请参阅官方文档

You should not be doing this in onReceive(Context, Intent) method as mentioned in official documentation

This is called for every broadcast and before each of the above callback methods. You normally don't need to implement this method because the default AppWidgetProvider implementation filters all App Widget broadcasts and calls the above methods as appropriate.

You should do this in onAppWidgetOptionsChanged().
See the official docs.

public class Showing extends AppWidgetProvider {

     private static boolean status = false;

     @Override
     public void onReceive(Context context, Intent intent) {
      super.onReceive(context, intent);
      if (intent.getAction()==null) {
             Bundle extras = intent.getExtras();
             if(extras!=null) {
                 remoteViews = new RemoteViews( context.getPackageName(), R.layout.your_widget_layout );
                 if(status){
                   remoteViews.setViewVisibility(R.id.buttonId,View.INVISIBLE);
                  status = false;
                 }else{
                   remoteViews.setViewVisibility(R.id.buttonId,View.VISIBLE);
                  status = true;
                }
                 watchWidget = new ComponentName( context, ButtonHideShowWidget.class );
                 (AppWidgetManager.getInstance(context)).updateAppWidget( watchWidget, remoteViews );
                 //Toast.makeText(context, "Clicked "+status, 2000).show();
             }
          }
       }
    }
public class Showing extends AppWidgetProvider {

     private static boolean status = false;

     @Override
     public void onReceive(Context context, Intent intent) {
      super.onReceive(context, intent);
      if (intent.getAction()==null) {
             Bundle extras = intent.getExtras();
             if(extras!=null) {
                 remoteViews = new RemoteViews( context.getPackageName(), R.layout.your_widget_layout );
                 if(status){
                   remoteViews.setViewVisibility(R.id.buttonId,View.INVISIBLE);
                  status = false;
                 }else{
                   remoteViews.setViewVisibility(R.id.buttonId,View.VISIBLE);
                  status = true;
                }
                 watchWidget = new ComponentName( context, ButtonHideShowWidget.class );
                 (AppWidgetManager.getInstance(context)).updateAppWidget( watchWidget, remoteViews );
                 //Toast.makeText(context, "Clicked "+status, 2000).show();
             }
          }
       }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文