iT邦幫忙

0

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

  • 分享至 

  • xImage
  •  

將〈Android 算法:遍历ViewGroup找出所有子View〉一文所示,在Visual Studio 2019中改用C#來寫
感恩感恩 南無阿彌陀佛

全程式碼詳:https://github.com/oscarsun72/udemy-cs-guide-learn-lecture-PYDOING/blob/master/GuessGameAndroidPractise/GuessGameAndroidPractise/GameActivity.cs
實境秀見:https://youtu.be/OJxVgN4liQk

Yes

1.遞歸(recursion)方式

        int traverseViewGroup_recursion_EnabledFalse(View view)
        {
            if (view == null) return 0;
            int viewCount = 0;//宣告應該在遞歸(recursion)時不會被覆寫,因其有int冠前也
            //https://docs.microsoft.com/zh-tw/dotnet/api/system.type.isinstanceoftype?view=netcore-3.1
            var abstractType = typeof(ViewGroup);
            if (abstractType.IsInstanceOfType(view))
            {
                ViewGroup vg = (ViewGroup)view;//要取用ChildCount屬性、GetChildAt方法須轉型為子類別,子類別ViewGroup才有定義此屬性、方法
                for (int i = 0; i < vg.ChildCount; i++)
                {
                    View vw = vg.GetChildAt(i);
                    if (abstractType.IsInstanceOfType(vw))
                    {
                        //遞歸(recursion)
                        viewCount +=
                            traverseViewGroup_recursion_EnabledFalse(vw);
                    }
                    else
                    {
                        viewCount++;
                        var contentDescription = vw.ContentDescription;
                        if (contentDescription != null && 
                            contentDescription.IndexOf("buttonNum") > -1)
                            vw.Enabled = false;

                    }
                }
            }
            else
            {
                viewCount++;
                var contentDescription = view.ContentDescription;
                if (contentDescription != null 
                    && contentDescription.IndexOf("buttonNum") > -1)
                    view.Enabled = false;
            }
            return viewCount;

        }

2.非遞歸方式

        int traverseViewGroup_EnabledFalse(View view)
        {
            if (view == null) return 0;
            int viewCount = 0;
            var abstractType = typeof(ViewGroup);
            if (abstractType.IsInstanceOfType(view))
            {
                List<ViewGroup> lVg = new List<ViewGroup>();
                ViewGroup vg = (ViewGroup)view;
                lVg.Add(vg);
                while (lVg.Count() > 0)
                {
                    vg = lVg[0];
                    lVg.RemoveAt(0);
                    for (int i = 0; i < vg.ChildCount; i++)
                    {
                        var vw = vg.GetChildAt(i);
                        if (abstractType.IsInstanceOfType(vw))
                        {
                            lVg.Add((ViewGroup)vw);
                        }
                        else
                        {
                            viewCount++;
                            var contentDescription = vw.ContentDescription;
                            if (contentDescription != null &&
                                contentDescription.IndexOf("buttonNum") > -1)
                                vw.Enabled = false;
                        }
                    }

                }
            }
            else
            {
                viewCount++;
                var contentDescription = view.ContentDescription;
                if (contentDescription != null &&
                    contentDescription.IndexOf("buttonNum") > -1)
                    view.Enabled = false;
            }

            return viewCount;
        }


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言