使用Button使布局可见/不可见

时间:2023-01-17 13:39:19

At the moment I am working on a project for a graphical user interface on an Android tablet. On one screen I have several buttons, which I want to make a RelativeLayout visible/invisible with.

目前我正在开发Android平板电脑上的图形用户界面项目。在一个屏幕上,我有几个按钮,我想让RelativeLayout可见/不可见。

I tried using an onClickListener, but in the inner method onClick doesn't support non final variables, which I use to select each button and RelativeLayout.

我尝试使用onClickListener,但在内部方法onClick不支持非最终变量,我用它来选择每个按钮和RelativeLayout。

The GUI is built dynamically, as its whole structure depends on the data it gets fed via an XML file. Also the RelativeLayout uses TextViews which receive an update of their textes (sensordata like temperature and humidity), which is why a dynamic approach is used.

GUI是动态构建的,因为它的整个结构取决于它通过XML文件提供的数据。此外,RelativeLayout使用TextViews接收其文本的更新(sensordata,如温度和湿度),这就是使用动态方法的原因。

Could you give me some ideas for a workaround around that problem? Help is appreciated. If the stated information is not enough for you, just ask and I will give you more details.

你能否就这个问题的解决方法给我一些想法?感谢帮助。如果说明的信息不够,请询问,我会向您提供更多详细信息。

At the moment I tried this:

目前我试过这个:

private void setSensorPointOnClick(final ObjectView currentObjectView, final String currentLinkName)
{
    for(int i=0; i<listofSensorDeviceButtons.size(); i++ )
    {

        listofSensorDeviceButtons.get(i).setOnClickListener(
            new View.OnClickListener()  
            {
                public void onClick(View v)  
                {
                    arrayClickedButton[i] = listofSensorDeviceButtons.get(i);
                    mainFrameLayout.removeAllViews();
                    stepToObject(currentObjectView, currentObjectView.getName());               
                }
            }
        );

    }
}

1 个解决方案

#1


0  

either use fields which hold the views that you want to hide/show (and use them in the onClickListener) , or use findViewById within the event handling .

要么使用包含要隐藏/显示的视图的字段(并在onClickListener中使用它们),要么在事件处理中使用findViewById。

another alternative would be to create another view variable which is final , that will be set just before setting the onClickListener , to hold the reference of the newly created view .

另一种方法是创建另一个最终的视图变量,它将在设置onClickListener之前设置,以保存新创建的视图的引用。

so , in short , the possible solutions i wrote are:

所以,简而言之,我写的可能的解决方案是:

  1. use fields .
  2. 使用字段。

  3. use findViewById
  4. use an additional final variable .
  5. 使用额外的最终变量。

#1


0  

either use fields which hold the views that you want to hide/show (and use them in the onClickListener) , or use findViewById within the event handling .

要么使用包含要隐藏/显示的视图的字段(并在onClickListener中使用它们),要么在事件处理中使用findViewById。

another alternative would be to create another view variable which is final , that will be set just before setting the onClickListener , to hold the reference of the newly created view .

另一种方法是创建另一个最终的视图变量,它将在设置onClickListener之前设置,以保存新创建的视图的引用。

so , in short , the possible solutions i wrote are:

所以,简而言之,我写的可能的解决方案是:

  1. use fields .
  2. 使用字段。

  3. use findViewById
  4. use an additional final variable .
  5. 使用额外的最终变量。