【问题标题】:Set button to GONE initially?最初将按钮设置为 GONE?
【发布时间】:2012-03-13 02:22:52
【问题描述】:

我需要这个错过按钮在初始屏幕上消失,然后在切换按钮切换时出现,并在重新切换时再次消失。

atm 一直存在的问题是按钮最初在初始屏幕上没有消失,我必须按两次切换按钮才能消失。

代码如下:

toggle = (ToggleButton) findViewById(R.id.bRedGreen);
    toggle.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            pluskugle = (Button) findViewById(R.id.bBallhole);
            minuskugle = (Button) findViewById(R.id.bBallhole);

            pluskegle = (Button) findViewById(R.id.bKegle);
            minuskegle = (Button) findViewById(R.id.bKegle);

            plusmidkegle = (Button) findViewById(R.id.bKeglemid);
            minusmidkegle = (Button) findViewById(R.id.bKeglemid);

            bottomlayout = (LinearLayout) findViewById(R.id.bottomlayout);

            miss = (Button) findViewById(R.id.bMiss);

        if(toggle.isChecked())
        {
            minuskugle.setBackgroundResource(R.drawable.redballinhole);
            minuskegle.setBackgroundResource(R.drawable.redkegle);
            minusmidkegle.setBackgroundResource(R.drawable.midkegleminus);
            miss.setBackgroundResource(R.drawable.missbutton);

            miss.setVisibility(View.VISIBLE);

        }

        else 
        {
            pluskugle.setBackgroundResource(R.drawable.whiteballinhole);
            pluskegle.setBackgroundResource(R.drawable.kegleb);
            plusmidkegle.setBackgroundResource(R.drawable.midkegleplus);
            miss.setVisibility(View.GONE);

        }

        }

    });
}

【问题讨论】:

  • layout.xml中的按钮是怎么定义的? android:visibility="GONE"?

标签: android android-layout button togglebutton


【解决方案1】:

您可以使用 android:visibility="gone" 设置其在布局中的可见性

【讨论】:

    【解决方案2】:

    您看到的行为是两件事的结果:

    -按钮最初可见的原因是您的 XML 中没有 android:visibility="gone",并且设置它不可见的代码尚未触发,因为尚未发生任何点击。

    -您必须按两次切换按钮才能将其设置为不可见的原因:第一次单击将切换按钮从未选中切换到选中,因此代码将按钮(已经可见)设置为可见。第二次单击是第一次触发 onClick 并且未选中切换按钮。那是你真正看到按钮不可见的时候。

    要解决此问题,只需将“android:visibility="GONE" 作为属性添加到布局 XML 中的未命中按钮即可。

    【讨论】:

      【解决方案3】:

      bMiss的xml中

      添加android:visibility:"gone"

      【讨论】:

        【解决方案4】:

        我建议您将所有 findViewById 方法从切换 onClick 方法移动到您的 onCreate 或初始化方法。然后在你的:

        miss = (Button) findViewById(R.id.bMiss);

        放:

        miss.setVisibility(View.INVISIBLE);

        miss.setVisibility(View.GONE);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-15
          • 2013-08-07
          • 1970-01-01
          相关资源
          最近更新 更多