如何解决,当我们使用多视图时键盘会自动隐藏吗?

时间:2018-10-29 07:27:05

标签: xamarin xamarin.ios

我们有两个将动态显示的搜索文本框。一个用于肖像视图,第二个用于风景视图。我们使用ViewDidLayoutSubviews()方法中的flag处理输入文本。我们编写了一个键盘隐藏通知方法,称为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();

    }

1 个答案:

答案 0 :(得分:0)

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

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

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