xamarin - 如何解决,当我们使用多个视图时,键盘会自动隐藏?
问题描述
我们有两个动态显示的搜索文本框。一个用于纵向视图,第二个用于横向视图。我们在 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();
}
解决方案
我们已经通过使用标志在逻辑上解决了它。isKeyboardAppear=false
当视图首先出现时,我们已经声明了一个布尔标志。当键盘出现时,我们将此标志设置为 true 并且我们注意到 ViewDidLayoutSubviews() 方法在每次按键时都会调用,所以我们编写了这段代码
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
if (isKeyboardAppear == true)
{
return;
}
}
当我们手动隐藏键盘时,我们重置标志 isKeyboardAppear=false 。
推荐阅读
- python - 使用 Fiona Python 向 shapefile 的属性表添加新列
- sqlalchemy - 如何从 SQLAlchemy 中的子表中过滤数据?
- join - 如何在 ClickHouse 上创建类似于使用 LIKE 和 JOIN 的 MySQL 查询的查询?
- c - 将 3 个点参数传递给另一个函数
- json - 如何以编程方式向我的 json 对象添加新字段?
- mysql - MariaDB 在 Docker 容器中的持久设置
- sonarqube - 带有混合单元测试文件和源文件的 SonarQube
- python - 未定义 OMSNX 名称“_index_deprecate”
- macos-big-sur - 搜索 Google Chrome macOS 服务
- php - 正确转换 PHP 和 SQL 数据类型