禁用 UI 键盘中的特定键

发布于 2024-12-03 03:05:31 字数 58 浏览 1 评论 0原文

它需要禁用“&”数字和标点符号键盘中的键,那么是否可以禁用 UIKeyboard 中的特定键?

It need to disable '&' key from number and punctuation keyboard, so is it possible to disable a particular key in UIKeyboard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝眼泪 2024-12-10 03:05:31

我认为不可能禁用某个键(除非它是操作键之一,例如返回键),但如果您使用的是 UITextField,则可以使用 - (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 委托方法来查看用户是否按下了 & key 并将其从字符串中删除

I don't think it's possible to disable a certain key (unless it's one of the action keys such as the return key) but if you are using a UITextField you can use the - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string delegate method to see if the user pressed the & key and remove it from the string

再浓的妆也掩不了殇 2024-12-10 03:05:31

你不能那样做。但是您的选择是:

  • 创建您自己的自定义键盘,不提供“&” key(在我看来,太费力了)
  • 如果您使用 UITextField 您可以验证用户提交的文本:删除 '&'和/或通知用户不允许使用“&” (更容易)。

编辑:您还可以将 UITextField 的“编辑已更改”事件连接到文件所有者的 IBAction 并过滤掉“&”那里。

You cannot do that. However your options are:

  • create your own custom keyboard not offerring '&' key (too much effort IMO)
  • If you use UITextField you can validate the text submitted by user: remove '&' and/or inform user that it is not allowed to use '&' (much easier).

EDIT: you can also connect UITextField's "Editing Changed" event to the File's Owner's IBAction and filter out '&' there.

野鹿林 2024-12-10 03:05:31

textField 有一种委托方法,您可以根据需要根据特定字符的 ASCII 值来阻止特定字符。该方法可以写如下:

-(BOOL)keyboardInput:(id)k shouldInsertText:(id)i isMarkedText:(int)b 
{
char s=[i characterAtIndex:0];
  if(selTextField.tag==1)
    {
        if(s>=48 && s<=57 && s == 38)  // 48 to 57 are the numbers and 38 is the '&' symbol
        {
            return YES;
        }
         else
        {
            return NO;
        }

    }
}

该方法只允许数字和 & 。由用户输入的符号。即使用户按下其他字符,它们也不会被输入。由于它是 textField 的委托方法,因此您无需担心显式调用它。

There is one delegate method for textField in which you can block specific characters if you want based on their ASCII values. The method can be written as follows:

-(BOOL)keyboardInput:(id)k shouldInsertText:(id)i isMarkedText:(int)b 
{
char s=[i characterAtIndex:0];
  if(selTextField.tag==1)
    {
        if(s>=48 && s<=57 && s == 38)  // 48 to 57 are the numbers and 38 is the '&' symbol
        {
            return YES;
        }
         else
        {
            return NO;
        }

    }
}

This method will permit only numbers and & symbol to be entered by the user. Even if the user presses other characters they won't be entered. And as it is a textField's delegate method you don't need to worry about calling it explicitly.

白昼 2024-12-10 03:05:31
//Disabling the '<' '>' special characters key in Keyboard in my code

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSCharacterSet *nonNumberSet = [NSCharacterSet characterSetWithCharactersInString:@"<>"];
    if (range.length == 1)
        return YES;
    else
        return ([text stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
    return YES;
}
//Disabling the '<' '>' special characters key in Keyboard in my code

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSCharacterSet *nonNumberSet = [NSCharacterSet characterSetWithCharactersInString:@"<>"];
    if (range.length == 1)
        return YES;
    else
        return ([text stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文