is there a way to change edittext settings to both decimal (3.4) and singed(+/-)? and what tipe of variable should i set in my activity? I tried with decimal, number and signed but i want to use a number like -3.6 and to store it in my activity.
有没有办法将edittext设置更改为十进制(3.4)和烧结(+/-)?我应该在活动中设置什么样的变量?我尝试使用十进制,数字和签名,但我想使用像-3.6这样的数字并将其存储在我的活动中。
1 个解决方案
#1
2
In your activity class:
在您的活动类中:
EditText editText = (EditText)findViewById(R.id.editText);
editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
from InputType | Android Developers
来自InputType | Android开发者
__________________________________________________________________________________
OR:
In your activity class:
在您的活动类中:
EditText editText = (EditText)findViewById(R.id.editText);
editText.setKeyListener(DigitsKeyListener.getInstance("0123456789.-"));
That allows the EditText
to input decimals and negative signs, as you can see at the end of the line.
这允许EditText输入小数和负号,正如您在行尾看到的那样。
__________________________________________________________________________________
OR:
In your EditText
XML properties, add this property:
在EditText XML属性中,添加以下属性:
android:inputType="numberSigned|numberDecimal"
__________________________________________________________________________________
You can input the number to a String
to store the value inputted:
您可以将数字输入到String以存储输入的值:
EditText editText = (EditText)findViewById(R.id.editText);
String userInput = editText.getText().toString();
userInput
will then be equal to the String that the user inputted. To convert it to a double, you can do this:
userInput将等于用户输入的String。要将其转换为double,您可以执行以下操作:
// however, this will break your app if you convert an empty String to a double
// so if there could be no text in the EditText, use a try-catch
double userInputDouble = Double.parseDouble(editText.getText().toString());
#1
2
In your activity class:
在您的活动类中:
EditText editText = (EditText)findViewById(R.id.editText);
editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
from InputType | Android Developers
来自InputType | Android开发者
__________________________________________________________________________________
OR:
In your activity class:
在您的活动类中:
EditText editText = (EditText)findViewById(R.id.editText);
editText.setKeyListener(DigitsKeyListener.getInstance("0123456789.-"));
That allows the EditText
to input decimals and negative signs, as you can see at the end of the line.
这允许EditText输入小数和负号,正如您在行尾看到的那样。
__________________________________________________________________________________
OR:
In your EditText
XML properties, add this property:
在EditText XML属性中,添加以下属性:
android:inputType="numberSigned|numberDecimal"
__________________________________________________________________________________
You can input the number to a String
to store the value inputted:
您可以将数字输入到String以存储输入的值:
EditText editText = (EditText)findViewById(R.id.editText);
String userInput = editText.getText().toString();
userInput
will then be equal to the String that the user inputted. To convert it to a double, you can do this:
userInput将等于用户输入的String。要将其转换为double,您可以执行以下操作:
// however, this will break your app if you convert an empty String to a double
// so if there could be no text in the EditText, use a try-catch
double userInputDouble = Double.parseDouble(editText.getText().toString());