无法动态设置 setVisibility() 参数 [英] Not able to dynamically set the setVisibility() parameter

查看:16
本文介绍了无法动态设置 setVisibility() 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按如下方式设置按钮的可见性:

I am trying to set the visibility for a button as follows:

public Bundle setActivityState(Bundle bundle){
    startBtn = (Button) findViewById(R.id.startSensorsBtn);

    startBtn.setVisibility(
            getVisibilityState(bundle, PersistanceConstants.START_BTN_STATE)
    );          

    return bundle;
}

public int getVisibilityState(Bundle bundle, String keyName){
    if (bundle.getInt(keyName) == View.VISIBLE){
        return View.VISIBLE;
    } else if (bundle.getInt(keyName) == View.INVISIBLE){
        return View.INVISIBLE;
    } else if (bundle.getInt(keyName) == View.GONE){
        return View.GONE;
    }

    return 0;
}

但我收到错误:

Must be one of: View.VISIBLE, View.INVISIBLE, View.GONE less... (Ctrl+F1) 
Reports two types of problems:
- Supplying the wrong type of resource identifier. For example, when calling Resources.getString(int id), you should be passing R.string.something, not R.drawable.something.
- Passing the wrong constant to a method which expects one of a specific set of constants. For example, when calling View#setLayoutDirection, the parameter must be android.view.View.LAYOUT_DIRECTION_LTR or android.view.View.LAYOUT_DIRECTION_RTL.

通话时

getVisibilityState(bundle, PersistanceConstants.START_BTN_STATE)

我不知道如何解决这个问题.我知道它期待一组给定的值,但我所知道的是将 int 传递给它.在这里可以做什么?

I don't know how to get around this. I understand that it's expecting a given set of values but all I know is to pass an int to it. What can be done here?

推荐答案

当你知道自己在做什么时,你可以使用

When you know what you're doing, you can suppress this Android Studio inspection locally with

//noinspection ResourceType

例如

//noinspection ResourceType
startBtn.setVisibility(bundle.getInt(PersistanceConstants.START_BTN_STATE));

这篇关于无法动态设置 setVisibility() 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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