在Xamarin Forms Android中删除搜索栏文本下划线,同时聚焦/键入

vmpqdwk3  于 2022-12-07  发布在  Android
关注(0)|答案(1)|浏览(101)

我想删除在聚焦/键入时出现在文本下面的行(而不是android:id/search_plate),但我无法删除它。

  • 这可能吗?
  • 如果可能的话,我该怎么做呢?
  • 我应该读什么书来学习做这件事?

背景:

  • 我们在谈论这个:

  • 当然,我们在这里讨论的是自定义渲染。

  • 我试过什么了?

SO Solution 1
SO Solution 2
我的程式码外观:

public class CustomSearchBarRenderer : SearchBarRenderer
   {
       public CustomSearchBarRenderer(Context context) : base(context)
       {
       }

       protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
       {
           base.OnElementChanged(e);

           SearchView searchView = Control;
           searchView.SetInputType(InputTypes.ClassText | InputTypes.TextVariationNormal);

           int textViewId = searchView.Context.Resources.GetIdentifier("android:id/search_src_text", null, null);
           EditText textView = (searchView.FindViewById(textViewId) as EditText);

           textView.SetBackgroundColor(G.Color.ParseColor("#FFFFFF"));
           textView.SetTextColor(G.Color.ParseColor("#191E24"));
           textView.SetHintTextColor(G.Color.ParseColor("#191E24"));
           textView.SetTextSize(Android.Util.ComplexUnitType.Dip, 16);
           textView.SetPadding(8, 0, 40, 0);

           int searchIconId = Context.Resources.GetIdentifier("android:id/search_mag_icon", null, null);
           ImageView searchViewIcon = searchView.FindViewById<ImageView>(searchIconId);
           searchViewIcon.SetImageDrawable(null);

           int searchCloseIconId = Context.Resources.GetIdentifier("android:id/search_close_btn", null, null);
           ImageView searchClose = searchView.FindViewById<ImageView>(searchCloseIconId);
           searchClose.SetPadding(8, 0, 8, 0);

           searchView.Iconified = false;
           searchView.SetIconifiedByDefault(false);

           //new code, did not work
           //this.Control.SetInputType(InputTypes.Null);

           //new code, did not work
           //GradientDrawable gd = new GradientDrawable();
           //gd.SetColor(Android.Graphics.Color.Transparent);
           //this.Control.SetBackground(gd);

           //new code, did not work
           //var plateId = Resources.GetIdentifier("android:id/search_plate", null, null);
           //var plate = Control.FindViewById(plateId);
           //plate.SetBackgroundColor(Android.Graphics.Color.Green);

           //new code, did not work
           //LinearLayout linearLayout = this.Control.GetChildAt(0) as LinearLayout;
           //linearLayout = linearLayout.GetChildAt(2) as LinearLayout;
           //linearLayout = linearLayout.GetChildAt(1) as LinearLayout;
           //linearLayout.Background = null; //removes underline
       }

   }
44u64gxh

44u64gxh1#

该文本下划线与键盘建议有关

Control?.SetInputType(InputTypes.TextFlagNoSuggestions | 
                      InputTypes.TextVariationVisiblePassword);

由于某些原因,标志TextFlagNoSuggestions单独不起作用,需要与TextVariationVisiblePassword结合使用。

相关问题