首页 > 解决方案 > 如何解决,当我们使用多个视图时,键盘会自动隐藏?

问题描述

我们有两个动态显示的搜索文本框。一个用于纵向视图,第二个用于横向视图。我们在 ViewDidLayoutSubviews() 方法中使用标志处理输入文本。我们编写了一个键盘隐藏通知方法作为 KeyboardWillHide(NSNotification notification) 用于文本值处理。

我们正在解决这些问题。请看一下 1. 当我们开始键入 ViewDidLayoutSubviews() 方法时,每次调用按键并重新加载文本框。所以我的键盘无法启动。如果键盘将返回,我们已通过检查标志来解决此问题。2.现在我们遇到了同样的问题,当我们按下麦克风说话写字时,键盘会自动隐藏。KeyboardWillHide(NSNotification notification) 方法每次调用并隐藏键盘。当我们触摸 tableview 时它也会调用。如何解决这些问题,请帮忙

 private void KeyboardWillHide(NSNotification notification)
    {

      string orientation = this.InterfaceOrientation.ToString();
            if ((orientation.ToString() == "Portrait" || orientation.ToString() == "PortraitUpsideDown") && KeyboardHideSameView == false )
            {
                SearchText= txtSearchKeywordLS.Text;
                txtSearchKeyword.Text = txtSearchKeywordLS.Text;

            }
            else if ((orientation.ToString() == "LandscapeRight" || orientation.ToString() == "LandscapeLeft") && KeyboardHideSameView == false )
            {
                SearchText = txtSearchKeyword.Text;
                txtSearchKeywordLS.Text = txtSearchKeyword.Text;

            }

            txtSearchKeyword.ResignFirstResponder();
            txtSearchKeywordLS.ResignFirstResponder();
            isKeyboardApear = false;
            KeyboardHideSameView = false;
        }

public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();

        if (isKeyboardApear == true) return;
        applyViewGradient();

        var orientation = this.InterfaceOrientation;

        if (orientation.ToString() == "Portrait" || orientation.ToString() == "PortraitUpsideDown")
        {
            PortraitConst(this.View.Bounds.Height, this.View.Bounds.Width);

            this.vwLandscapeHead.Hidden = true;
            this.vwPortraitHead.Hidden = false;

            this.txtSearchKeyword.Text = SearchText;

            txtSearchKeyword.ResignFirstResponder();
            txtSearchKeywordLS.ResignFirstResponder();
        }
        else if (orientation.ToString() == "LandscapeRight" || orientation.ToString() == "LandscapeLeft")
        {
            //Console.WriteLine("Landscape Mode");


           // this.addSubview(landscapeView, containerView);

            LandscapeConst(UIScreen.MainScreen.Bounds.Height, UIScreen.MainScreen.Bounds.Width);

            applyViewGradient();

            this.vwPortraitHead.Hidden = true;
            this.vwLandscapeHead.Hidden = false;

            this.txtSearchKeywordLS.Text = SearchText;

            txtSearchKeyword.ResignFirstResponder();
            txtSearchKeywordLS.ResignFirstResponder();
        }
       // this.containerView.NeedsUpdateConstraints();

    }

标签: xamarinxamarin.ios

解决方案


我们已经通过使用标志在逻辑上解决了它。isKeyboardAppear=false当视图首先出现时,我们已经声明了一个布尔标志。当键盘出现时,我们将此标志设置为 true 并且我们注意到 ViewDidLayoutSubviews() 方法在每次按键时都会调用,所以我们编写了这段代码

public override void ViewDidLayoutSubviews() 
{
 base.ViewDidLayoutSubviews(); 
 if (isKeyboardAppear == true) 
 { 
  return; 
 } 
}

当我们手动隐藏键盘时,我们重置标志 isKeyboardAppear=false 。


推荐阅读