如何在 Android 中禁用/启用 LinearLayout 上的所有子项

发布于 2024-12-08 19:31:18 字数 846 浏览 0 评论 0原文

有没有办法通过编程来确定某个布局的所有子项?

例如,我有两个孩子的布局:

<LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:layout_width="fill_parent">
        <SeekBar android:layout_height="wrap_content" android:id="@+id/seekBar1"
            android:layout_weight="1" android:layout_width="fill_parent"></SeekBar>
        <TextView android:id="@+id/textView2" android:text="TextView"
            android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_height="wrap_content"></TextView>
    </LinearLayout>

并且我想做类似的事情:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearLayout1);
myLayout.setEnabled(false);

为了禁用两个文本视图。

知道怎么做吗?

Is there way by programming that all the children of a certain layout?

For example i have this layout with two children:

<LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:layout_width="fill_parent">
        <SeekBar android:layout_height="wrap_content" android:id="@+id/seekBar1"
            android:layout_weight="1" android:layout_width="fill_parent"></SeekBar>
        <TextView android:id="@+id/textView2" android:text="TextView"
            android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_height="wrap_content"></TextView>
    </LinearLayout>

and i want to do something like:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearLayout1);
myLayout.setEnabled(false);

In order to disable the two textviews.

Any idea how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遮云壑 2024-12-15 19:31:18

LinearLayout 扩展了 ViewGroup,因此您可以使用 getChildCount() 和 getChildAt(index) 方法来迭代 LinearLayout 子级并对它们执行任何您想要的操作。我不确定启用/禁用是什么意思,但如果您只是想隐藏它们,您可以执行 setVisibility(View.GONE);

所以,它看起来像这样:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearLayout1);
for ( int i = 0; i < myLayout.getChildCount();  i++ ){
    View view = myLayout.getChildAt(i);
    view.setVisibility(View.GONE); // Or whatever you want to do with the view.
}

A LinearLayout extends ViewGroup, so you can use the getChildCount() and getChildAt(index) methods to iterate through your LinearLayout children and do whatever you want with them. I'm not sure what you mean by enable/disable, but if you're just trying to hide them, you can do setVisibility(View.GONE);

So, it would look something like this:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearLayout1);
for ( int i = 0; i < myLayout.getChildCount();  i++ ){
    View view = myLayout.getChildAt(i);
    view.setVisibility(View.GONE); // Or whatever you want to do with the view.
}
软的没边 2024-12-15 19:31:18

您还可以在不使用 setVisibility() 的情况下禁用/启用

将 View.OnClickListener 添加到您的 CheckBox,然后将要禁用的视图传递到以下函数中...

private void enableDisableView(View view, boolean enabled) {
    view.setEnabled(enabled);

    if ( view instanceof ViewGroup ) {
        ViewGroup group = (ViewGroup)view;

        for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) {
            enableDisableView(group.getChildAt(idx), enabled);
        }
    }
}

使用以下参考 有吗一种以编程方式禁用特定布局中所有项目的方法?

You can also disable/enable without using setVisibility()

Add a View.OnClickListener to your CheckBox then pass the View you want to be disabled into the following function...

private void enableDisableView(View view, boolean enabled) {
    view.setEnabled(enabled);

    if ( view instanceof ViewGroup ) {
        ViewGroup group = (ViewGroup)view;

        for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) {
            enableDisableView(group.getChildAt(idx), enabled);
        }
    }
}

with the following reference Is there a way to disable all the items in a specific layout programmaticaly?

-残月青衣踏尘吟 2024-12-15 19:31:18

为什么不只在布局本身上执行 setVisibility(View.GONE) ,而不是迭代子级?

Why not just doing the setVisibility(View.GONE) on the layout itself, rather than iterating through the children?

谢绝鈎搭 2024-12-15 19:31:18

也许已经晚了,但您可以在 LinearLayout 的子级中添加 android:duplicateParentState="true"

如下所示:

 <LinearLayout
     android:id="@+id/container"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">

     <TextView
         android:id="@+id/textA"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:duplicateParentState="true"
         android:text="First Text"/>

     <TextView
         android:id="@+id/textB"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:duplicateParentState="true"
         android:text="Second Text"/>

 </LinearLayout>

Maybe is late, but you can just add android:duplicateParentState="true" in the child of the LinearLayout

Something like this:

 <LinearLayout
     android:id="@+id/container"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">

     <TextView
         android:id="@+id/textA"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:duplicateParentState="true"
         android:text="First Text"/>

     <TextView
         android:id="@+id/textB"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:duplicateParentState="true"
         android:text="Second Text"/>

 </LinearLayout>
擦肩而过的背影 2024-12-15 19:31:18

只需添加另一个透明布局,该布局将与原始视图的 match_parent 并在您想要禁用所有子视图并启用子视图时将其可见性更改为可见,然后只需将可见性更改为消失即可

Just add another transparent layout that will match_parent of the original view and change it visibility to visible when you want to disable all the children and to enable children then just change visibility to gone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文