另一种获得控件的方法public View getChildAt(int index)

本文介绍了一种在Android中获取未设置ID的控件的方法。通过使用LinearLayout的getChildAt(int index)方法,可以方便地获取到指定位置的子控件。此方法适用于那些不需要为每个控件单独设置ID的情况。

另一种获得控件的方法public View getChildAt(int index)

除了在布局文件里里面给控件设ID,然后通过findViewById(int id)方法得到控件外,还有另一种方法:

比如有这样一个布局文件

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

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

我们没有为2个TextView设置ID,但是照样可以得到它们,方法如下
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
TextView tv1 = (TextView) linearLayout.getChildAt(0);
TextView tv2 = (TextView) linearLayout.getChildAt(1);

用getChildAt方法也可以得到控件



package com.ethanhua.skeleton; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by ethanhua on 2017/8/2. */ public class ViewReplacer { private static final String TAG = ViewReplacer.class.getName(); private final View mSourceView; private View mTargetView; private int mTargetViewResID = -1; private View mCurrentView; private ViewGroup mSourceParentView; private final ViewGroup.LayoutParams mSourceViewLayoutParams; private int mSourceViewIndexInParent = 0; private final int mSourceViewId; public ViewReplacer(View sourceView) { mSourceView = sourceView; mSourceViewLayoutParams = mSourceView.getLayoutParams(); mCurrentView = mSourceView; mSourceViewId = mSourceView.getId(); } public void replace(int targetViewResID) { if (mTargetViewResID == targetViewResID) { return; } if (init()) { mTargetViewResID = targetViewResID; replace(LayoutInflater.from(mSourceView.getContext()).inflate(mTargetViewResID, mSourceParentView, false)); } } public void replace(View targetView) { if (mCurrentView == targetView) { return; } if (targetView.getParent() != null) { ((ViewGroup) targetView.getParent()).removeView(targetView); } if (init()) { mTargetView = targetView; mSourceParentView.removeView(mCurrentView); mTargetView.setId(mSourceViewId); mSourceParentView.addView(mTargetView, mSourceViewIndexInParent, mSourceViewLayoutParams); mCurrentView = mTargetView; } } public void restore() { if (mSourceParentView != null) { mSourceParentView.removeView(mCurrentView); mSourceParentView.addView(mSourceView, mSourceViewIndexInParent, mSourceViewLayoutParams); mCurrentView = mSourceView; mTargetView = null; mTargetViewResID = -1; } } public View getSourceView() { return mSourceView; } public View getTargetView() { return mTargetView; } public View getCurrentView() { return mCurrentView; } private boolean init() { if (mSourceParentView == null) { mSourceParentView = (ViewGroup) mSourceView.getParent(); if (mSourceParentView == null) { Log.e(TAG, "the source view have not attach to any view"); return false; } int count = mSourceParentView.getChildCount(); for (int index = 0; index < count; index++) { if (mSourceView == mSourceParentView.getChildAt(index)) { mSourceViewIndexInParent = index; break; } } } return true; } } 解释这个代码
最新发布
09-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值