简体   繁体   English

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

[英]How to Hide/Show button in android home screen widget

I am a beginner of android development. 我是android开发的初学者。 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. 设置壁纸工作正常,但我想制作一个可点击的小图片(ImageView),以允许用户显示和隐藏此设置按钮。

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. 我在服务上设置它并使用PendingIntent将我的onClick事件附加到同一个服务,但我无法检测按钮的属性是显示还是隐藏。

Therefore,is there any suggestion and solution to make my ImageView to show or hide the button in home screen widget? 因此,是否有任何建议和解决方案使我的ImageView显示或隐藏主屏幕小部件中的按钮?

Thanks in advance.. 提前致谢..

You can use mButton.setVisibility(View.GONE) to hide the button. 您可以使用mButton.setVisibility(View.GONE)来隐藏按钮。

You can also check state of button's visibility in a boolean variable using mButton.isShown(). 您还可以使用mButton.isShown()在布尔变量中检查按钮的可见性状态。

Edited: 编辑:

For Example 例如

In onReceive() of AppWidgetProvider , AppWidgetProvider onReceive()中,

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

     remoteViews.setViewVisibility(viewId, visibility);

So for hiding your Button 所以隐藏你的Button

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

Edit - 2: According to Kartik's comment, 编辑 - 2:根据Kartik的评论,

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();
             }
         }
    }
}
// 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);

Call setVisibility(View.Invisible); 调用setVisibility(View.Invisible); with the help of button object created by you after the user clicks the button. 在用户单击按钮后,在您创建的按钮对象的帮助下。

You should not be doing this in onReceive(Context, Intent) method as mentioned in official documentation 您不应该在官方文档中提到的onReceive(Context,Intent)方法中执行此操作

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. 您通常不需要实现此方法,因为默认的AppWidgetProvider实现过滤所有App Widget广播并根据需要调用上述方法。

You should do this in onAppWidgetOptionsChanged(). 您应该在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();
             }
          }
       }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM