【问题标题】:How to hide a button programmatically?如何以编程方式隐藏按钮?
【发布时间】:2011-09-04 15:05:48
【问题描述】:

我有一个RelativeLayout,其中包含两个按钮。它们相互重叠。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">


<Button android:text="Play"  
    android:id="@+id/play"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom = "true">
</Button>

<Button android:text="Stop "
    android:id="@+id/stop" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_alignParentBottom = "true">
</Button>


</RelativeLayout>

我想在调用其点击事件时以编程方式一次只显示一个按钮。

我试过了:

playButton.setVisibility(1);

但它不起作用。以下是我正在尝试做的一个例子。

playButton = (Button) findViewById(R.id.play);
playButton.setVisibility(1);
playButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        //when play is clicked show stop button and hide play button

    }
});

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以使用以下代码:

    playButton = (Button) findViewById(R.id.play);
    playButton.setVisibility(View.VISIBLE);
    playButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //when play is clicked show stop button and hide play button
            playButton.setVisibility(View.GONE);
            stopButton.setVisibility(View.VISIBLE);
        }
    });
    

    【讨论】:

    • 谢谢 sunil :) 你能说出 View.VISIBLe 和 1 之间的区别吗(只是枚举)?
    • 为什么将可见性设置为 1?这不是任何常量值。
    • View.GONE 使项目不占用任何布局空间。 View.INVISIBLE 为项目保留空间。当您切换可见性时,这会更改视图的布局。
    【解决方案2】:

    试试下面的代码 -

    playButton.setVisibility(View.INVISIBLE);
    

    或 -

    playButton.setVisibility(View.GONE);
    

    用 - 再次显示它

    playButton.setVisibility(View.VISIBLE);
    

    【讨论】:

      【解决方案3】:

      在科特林中

      myButton.visibility = View.GONE

      【讨论】:

        【解决方案4】:

        隐藏:

        BUTTON.setVisibility(View.GONE);
        

        显示:

        BUTTON.setVisibility(View.VISIBLE);
        

        【讨论】:

          【解决方案5】:

          请在下面使用

          View.GONE and View.VISIBLE
          

          【讨论】:

            【解决方案6】:
            public void OnClick(View.v)
            Button b1 = (Button) findViewById(R.id.playButton);
            b1.setVisiblity(View.INVISIBLE);
            

            【讨论】:

              【解决方案7】:

              我建议您只使用一个按钮来按需更改按钮上的文本和行为。这比处理两个重叠的按钮更容易和更干净。

              @Override
              public void onClick(View v) {
                  String curText = ((TextView)v).getText();                 
              
                  if(curText.equals("Play")){
                      ((TextView)v).setText("Stop");
                  }
              
                  if(curText.equals("Stop")){
                      ((TextView)v).setText("Play");
                  }
               }
              

              【讨论】:

              • 我喜欢你的想法,它实际上是我在 iphone 中切换单个按钮来执行多项操作。但我是 android 新手,请你给我举个例子来说明如何做到这一点..
              【解决方案8】:
                      Button button = (Button) findViewById(R.id.myButton);
                      //set to visible
                      button.setVisibility(View.VISIBLE);
                      //set to invisble      
                      button.setVisibility(View.INVISIBLE);
                     //or
                      button.setVisibility(View.GONE);
              

              【讨论】:

                【解决方案9】:

                试试View.INVISIBLE

                【讨论】:

                  【解决方案10】:

                  Kotlin 代码要简单得多:

                  if(isVisable) {
                      clearButton.visibility = View.INVISIBLE
                  }
                  else {
                      clearButton.visibility = View.VISIBLE
                  }
                  

                  【讨论】:

                    【解决方案11】:

                    请试试这个:playButton = (Button) findViewById(R.id.play); playButton.setVisibility(View.INVISIBLE); 我想这样就可以了。

                    【讨论】:

                      【解决方案12】:

                      对于“Xamarin Android”:

                      FindViewById<Button>(Resource.Id.Button1).Visibility = ViewStates.Gone;
                      

                      【讨论】:

                        猜你喜欢
                        • 2014-08-07
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2013-01-16
                        • 1970-01-01
                        • 2011-01-16
                        • 2020-04-11
                        • 1970-01-01
                        相关资源
                        最近更新 更多