如何在 android studio 上使用可见性?

How to use visibility on android studio?

我想我在导航栏上有 4 个按钮。 第一个按钮包含一些数据或项目,其他按钮也包含一些数据或项目。

注意:所有四个按钮都在同一个 xml 文件下。

我想如果我点击第一个按钮,它会显示第一个按钮数据或项目,同时它会隐藏其他三个按钮数据或项目。我想对所有按钮使用相同的方法。

我该怎么做,请帮助我。

如果你想隐藏其他三个按钮,你可以使用 switch case,你可以点击按钮,点击一个按钮可以隐藏所有其他按钮。

   Button b1 = (Button) findViewById(R.id.button1);       
   Button b2 = (Button) findViewById(R.id.button2);
   Button b3 = (Button) findViewById(R.id.button3);
   Button b4 = (Button) findViewById(R.id.button4);

    b1.setOnClickListener(this)         
    b2.setOnClickListener(this)
    b3.setOnClickListener(this)
    b4.setOnClickListener(this)
 

当您定义 onclick 方法时,您将获得被点击视图的 ID。

public void onClick(View v) {
           // Perform action on click
          switch(v.getId()) {

          case R.id.button1: 
             b1.setVisibility(View.VISIBLE);
             b2.setVisibility(View.GONE);
             b3.setVisibility(View.GONE);
             b4.setVisibility(View.GONE);
             break;

        case R.id.button2: 
             b1.setVisibility(View.GONE);
             b2.setVisibility(View.VISIBLE);
             b3.setVisibility(View.GONE);
             b4.setVisibility(View.GONE);
             break;

         case R.id.button3: 
             b1.setVisibility(View.GONE);
             b2.setVisibility(View.GONE);
             b3.setVisibility(View.VISIBLE);
             b4.setVisibility(View.GONE);
             break;

       case R.id.button1: 
             b1.setVisibility(View.GONE);
             b2.setVisibility(View.GONE);
             b3.setVisibility(View.GONE);
             b4.setVisibility(View.VISIBLE);
             break;
     }
}

您可以通过像这样在 onClick 中使所有其他按钮可见性变得有趣来实现:

  1. 如果你在Activity

    override fun onCreate(savedInstanceState: Bundle?) {
    
       val b1 = findViewById(R.id.button1)      
       val b2 = findViewById(R.id.button2)
       val b3 = findViewById(R.id.button3)
       val b4 = findViewById(R.id.button4)
    
       b1.setOnClickListener {
          b1.visibility = View.VISIBLE
          b2.visibility = View.GONE
          b3.visibility = View.GONE
          b4.visibility = View.GONE
      }
    
      b2.setOnClickListener {
         b1.visibility = View.GONE
         b2.visibility = View.VISIBLE
         b3.visibility = View.GONE
         b4.visibility = View.GONE
      }
    
      b3.setOnClickListener {
         b1.visibility = View.GONE
         b2.visibility = View.GONE
         b3.visibility = View.VISIBLE
         b4.visibility = View.GONE
      }
    
      b4.setOnClickListener {
         b1.visibility = View.GONE
         b2.visibility = View.GONE
         b3.visibility = View.GONE
         b4.visibility = View.VISIBLE
      }
    }
    
  2. 如果你在Fragment中

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
       super.onViewCreated(view, savedInstanceState)
    
       val b1 = view.findViewById(R.id.button1)      
       val b2 = view.findViewById(R.id.button2)
       val b3 = view.findViewById(R.id.button3)
       val b4 = view.findViewById(R.id.button4)
    
       b1.setOnClickListener {
          b1.visibility = View.VISIBLE
          b2.visibility = View.GONE
          b3.visibility = View.GONE
          b4.visibility = View.GONE
       }
    
       b2.setOnClickListener {
          b1.visibility = View.GONE
          b2.visibility = View.VISIBLE
          b3.visibility = View.GONE
          b4.visibility = View.GONE
       }
    
       b3.setOnClickListener {
          b1.visibility = View.GONE
          b2.visibility = View.GONE
          b3.visibility = View.VISIBLE
          b4.visibility = View.GONE
       }
    
       b4.setOnClickListener {
          b1.visibility = View.GONE
          b2.visibility = View.GONE
          b3.visibility = View.GONE
          b4.visibility = View.VISIBLE
       }
    }
    

在此示例中,当您按下一个按钮时,他将可见,而所有其他按钮将从 UI 中消失。

您可以使用 INVISIBLE 而不是 GONE,不同之处在于在 GONE 中,视图不会在您的屏幕上“捕捉”space,而 INVISIBLE 可以,只是对用户不可见(有关更多信息阅读 this)

因为你想隐藏 data/items 而不是 buttons.if 你直接隐藏按钮。你不能点击它。 假设如果您单击 button1 并且它隐藏了其他按钮。你不能点击button2。 您必须将所有数据放在可以隐藏的子布局中。

Button b1 = (Button) findViewById(R.id.button1);       
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);
Button b4 = (Button) findViewById(R.id.button4);

RelativeLayout r1 = (RelativeLayout) findViewById(R.id.buttonlayout1);       
RelativeLayout r2 = (RelativeLayout) findViewById(R.id.buttonlayout2);
RelativeLayout r3 = (RelativeLayout) findViewById(R.id.buttonlayout3);
RelativeLayout r4 = (RelativeLayout) findViewById(R.id.buttonlayout4);

b1.setOnClickListener(this)         
b2.setOnClickListener(this)
b3.setOnClickListener(this)
b4.setOnClickListener(this)

hide/show onClick 方法中的视图

public void onClick(View v) {
           // Perform action on click
          switch(v.getId()) {

          case R.id.button1: 
             r1.setVisibility(View.VISIBLE);
             r2.setVisibility(View.GONE);
             r3.setVisibility(View.GONE);
             r4.setVisibility(View.GONE);
             break;

        case R.id.button2: 
             r1.setVisibility(View.GONE);
             r2.setVisibility(View.VISIBLE);
             r3.setVisibility(View.GONE);
             r4.setVisibility(View.GONE);
             break;

         case R.id.button3: 
             r1.setVisibility(View.GONE);
             r2.setVisibility(View.GONE);
             r3.setVisibility(View.VISIBLE);
             r4.setVisibility(View.GONE);
             break;

       case R.id.button4: 
             r1.setVisibility(View.GONE);
             r2.setVisibility(View.GONE);
             r3.setVisibility(View.GONE);
             r4.setVisibility(View.VISIBLE);
             break;
     }
}