How allow a specific character in Android numerical keyboard?

余生长醉 提交于 2019-12-25 02:15:20

问题


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.

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


回答1:


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

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);


来源:https://stackoverflow.com/questions/13635319/how-allow-a-specific-character-in-android-numerical-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!