Android 算法:遍歷巡覽ViewGroup找出所有子View[C# 整併並獨立出來,自訂FindViewsWithText方法]

这篇博客将C#版的遍历ViewGroup并查找所有子View的算法整合到独立类中,提供了一种替代Java findViewsWithText的方法。内容包括呼叫端GameActivity.cs的调用示例和被呼叫端MyViewGroup.cs的详细实现,适用于Android开发者进行UI元素查找。

緣起:把前面寫的二篇合併起來,存到一個獨立類別檔,以便日後調用

Android 算法:遍歷巡覽ViewGroup找出所有子View[C# 替代Java的findViewsWithText方法]

Android 算法:遍歷巡覽ViewGroup找出所有子View[C# 以Xml方法3種實作] GetElementsByTagName、SelectNodes

全部程式碼

實境秀

呼叫端:

GameActivity.cs

        void setNumBtnEnabled(bool setEnabled)
        {
            List<View> vList = new List<View>();
            MyViewGroup myViewGroup = new MyViewGroup(this);
            myViewGroup.FindViewsWithText(ref vList,
                MyViewGroup.whatTraverseMethodToBeRun.withoutRecursion);
            foreach (View item in vList)
            {
                item.Enabled = setEnabled;
            }
        }

被呼叫端:

MyViewGroup.cs

using Android.Content;
using Android.Views;
using Android.Util;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Android.Widget;
using System.Xml;
using Android.Content.Res;
using Android.App;

namespace GuessGameAndroidPractise
{
    public class MyViewGroup : ViewGroup
    {
        //Called from layout when this view should assign a size and position to each of its children.
        //https://docs.microsoft.com/zh-tw/dotnet/api/android.views.viewgroup.onlayout?view=xamarin-android-sdk-9#Android_Views_ViewGroup_OnLayout_System_Boolean_System_Int32_System_Int32_System_Int32_System_Int32_
        int _childCount = 0;

        public new int ChildCount { get => _childCount; set => _childCount = value; }        
        Context _context1; static Activity activity;
        public MyViewGroup(Context context) : base(context)
        {
            _context1 = context;
            activity = (Activity)_context1;//https://stackoverflow.com/questions/9891360/getting-activity-from-context-in-android
        }
        
        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            //throw new System.NotImplementedException();
            if (changed)
            {
                Left = l; Top = t;
                Right = r; Bottom = b;
            }
            else
            {
                Left = l; Top = t;
                Right = r; Bottom = b;
            }
        }

        public enum whatTraverseMethodToBeRun
        {
            xml, recursion, withoutRecursion
        }

        //巡覽遍歷想要找的View,並取得List<View>,以便於對此群組作統一的操作
        public void FindViewsWithText(//traverse_any_view_hierarchy_in_android
            ref List<View> vList,whatTraverseMethodToBeRun method,
            chooseAXmlMethodforTraverseViewGroup c
                = 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值