负数输入(EditText)变为无符号

时间:2011-10-04 19:40:54

标签: android numbers android-edittext

我正在做一个电缆长度计算器,我遇到负数问题。

EditTexts就像这样

<EditText android:layout_height="wrap_content" android:layout_weight="1" android:inputType="numberDecimal|numberSigned" android:layout_width="40px" android:id="@+id/rxmax">
        </EditText>

然后我像这样使用它们:

final EditText rxmax = (EditText) findViewById(R.id.rxmax);
double RXmax = new Double(rxmax.getText().toString());

我做了一个简单的计算后:

double OPBmax = TXmax - RXmax;

某处输入的负数变为正数。我猜是在toString对话,但我没有找到任何关于如何防止这种情况。

2 个答案:

答案 0 :(得分:4)

使用 android:digits="0123456789"

EX:

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:digits="0123456789"
        android:gravity="center"
        android:inputType="numberSigned"
        android:textColor="@color/black"
        android:textSize="@dimen/font_size_small" />

答案 1 :(得分:0)

我试试这个,它有效......

的活动:

double RXmax;
EditText rxmax;
TextView tv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    rxmax = (EditText) findViewById(R.id.rxmax);
    tv = (TextView) findViewById(R.id.text);
}

public void click(View v) {
    RXmax =  new Double(rxmax.getText().toString());
    tv.setText(Double.toString(RXmax));
}

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <EditText android:layout_height="wrap_content"
        android:layout_weight="1" android:inputType="numberDecimal|numberSigned"
        android:layout_width="40px" android:id="@+id/rxmax">
    </EditText>

    <TextView android:id="@+id/text" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />

    <Button android:layout_height="wrap_content" android:id="@+id/button1"
        android:layout_weight="1" android:text="Button" android:layout_width="wrap_content"
        android:onClick="click"></Button>

</LinearLayout>

如果我输入-2,点击后,TextView显示-2.0 ......

在Eclipse中尝试Project - &gt; Clean ...,然后选择你的项目......

相关问题