android中获得某个activity中的view

本文介绍了如何在Android应用程序中通过代码实现对特定Activity内的View进行操作,包括查找和控制,对于开发者进行界面交互编程具有实用价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获得指定activity中的VIEW,通过代码的形式

public class GetActivityView {
	
	private Activity context;
	private View view;
	
	public GetActivityView(Activity context){
		this.context = context;
	}
	
	public List<View> getAllChildViews() {
		View view = context.getWindow().getDecorView();
		return getAllChildViews(view);
	}

	public List<View> getAllChildViews(View view) {
		List<View> allchildren = new ArrayList<View>();
		if (view instanceof ViewGroup) {
			ViewGroup vp = (ViewGroup) view;
			for (int i = 0; i < vp.getChildCount(); i++) {
				View viewchild = vp.getChildAt(i);
				String name = viewchild.getClass().getSimpleName();
				System.out.println(name);
				allchildren.add(viewchild);
				allchildren.addAll(getAllChildViews(viewchild));
			}
		}
		return allchildren;
	}
	
	public List<View> getAllChildViewsByName(View view,String name) {
		List<View> allchildren = new ArrayList<View>();
		if (view instanceof ViewGroup) {
			ViewGroup vp = (ViewGroup) view;
			for (int i = 0; i < vp.getChildCount(); i++) {
				View viewchild = vp.getChildAt(i);
				String viewName = viewchild.getClass().getSimpleName();
				if(viewName.equals(name)){
					allchildren.add(viewchild);
					allchildren.addAll(getAllChildViews(viewchild));
				}
			}
		}
		return allchildren;
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值