如何在Android数字键盘中允许特定字符? [英] How allow a specific character in Android numerical keyboard?

查看:136
本文介绍了如何在Android数字键盘中允许特定字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个科学应用程序,并且我有一个EditText,因此我必须输入一个输入数值,所以inputType是"number | numberSigned | numberDecimal",但是问题是当我想输入科学计数法数字时像"45.6E05"一样,由于EditText的InputType,我无法输入"E"字符.

I'm developing an science app, and I have an EditText in wich I must enter an input numeric value, so the inputType is "number|numberSigned|numberDecimal", but the problem is when I want input a scientific notation number like "45.6E05", I can't input "E" character because of EditText's InputType.

当EditText聚焦时,如何允许"E"字符并仅显示数字键盘?

How can to allow "E" character and only to show a numerical keyboard when the EditText is focused?

推荐答案

无法显示带有额外字母E的数字键盘,解决该问题的两种方法是使按钮输入E到您的edittext或使用android:digits属性,例如:

There is no way to show the numerical keyboard with an extra letter E on it, two ways around this are either make a button that inputs E to your edittext or to use the android:digits attribute like such:

android:digits="1234567890E."

括号中的所有字符都是您希望用户能够输入的字符

Where all the characters in the brackets are the ones that you want the user to be able to input

要使用按钮进行操作,只需在其onClickListener中放入以下代码

To make do it using a button, in its onClickListener you just need to put the following code

edittext.setText(edittext.getText().toString() + "E"

另一方面,如果要在光标位置插入字符,则应尝试使用此字符(取自

In the other hand if you want to insert the character in the position of the cursor then you should try this (taken from here)

int start =edittext.getSelectionStart();
String s = "E";
edittext.getText().insert(start, s);

这篇关于如何在Android数字键盘中允许特定字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆