【问题标题】:Android using counter result to set visibility of ImageViewAndroid 使用计数器结果设置 ImageView 的可见性
【发布时间】:2012-07-22 06:38:47
【问题描述】:

我有一个计数器,我想让这个计数器的结果决定 ImageView 的可见性:

ImageView image_A_wrong = (ImageView) findViewById(R.id.IVimage_a_wrong);

ImageView 的 XML:

<ImageView android:src="@drawable/small_wrong"
     android:id="@+id/IVimage_a_wrong" 
     android:layout_width="48dp"
     android:layout_height="48dp"
     android:layout_marginLeft="-50dp"
     android:padding="0dp"
     android:visibility="invisible"/>

我已经尝试了几件事..这是我想要完成的一些伪代码

@Override
public void setVisible(boolean visible) {
    super.setVisible(visible);
    if (score == 0 ){
        image_A_wrong.getVisibility(View.VISIBLE);
    } else if(score == 1){
        image_A_wrong.getVisibility(View.VISIBLE);      
    }
}

我在 tuts 和 Adroid 的 Dev 页面上环顾四周,但似乎无法找到/理解适用于我需求的解决方案...thnx

【问题讨论】:

  • getVsibility 调用应该是 setVisibility 吗?另外你怎么打电话给setVisible?用真还是假?计数器是否更改 score 对象?我认为您需要发布更多代码...
  • 是的,你是对的,它应该是 setVisibility(View.VISIBLE);
  • 我认为 true false(如果可见或不可见)是用 (View.VISIBLE) 处理的,因为它在 XML 上是 android:visibility="invisible"score 对象只是一个分数,它反映/显示通过捆绑从另一个活动计数器传递的 int...thnx

标签: android imageview visibility counter


【解决方案1】:

所以View#VISIBLE 将其标记为可见,View#INVISIBLE 将其标记为不可见,View#GONE 将视图完全隐藏。 这是您需要对代码执行的操作:

@Override
public void setVisible(boolean visible) {
    super.setVisible(visible);
    if (score == 0 ){
        //Hide the view
        image_A_wrong.setVisibility(View.INVISIBLE);
    } else if(score == 1){
        //Show the view
        image_A_wrong.setVisibility(View.VISIBLE);      
    }
}

我建议阅读View#setVisibility 文档以获得更好的想法。

编辑:我也不知道您在上面的代码中显示的 View#setVisible(boolean) 来自哪里。这不是我所知道的View 中的一个函数...

【讨论】:

    【解决方案2】:

    你应该使用 setVisibility() 方法而不是 getVisibility():

        imageview.setVisibility(View.VISIBLE);
    

    【讨论】:

    • 是的,你是对的 thnx..它是 imageview.setVisibility(View.VISIBLE); 我只是不知道我应该使用什么方法来检查计数器结果的 swtich and caseif 语句并取决于结果,使位图/可绘制可见或不可见。我可以将 onClickLister 分配给检查计数器结果的按钮。但我宁愿在活动开始后对其进行“检查”/运行..thnx
    猜你喜欢
    • 2013-05-01
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 2019-12-09
    • 2011-10-23
    • 2016-01-09
    相关资源
    最近更新 更多