Losó Adam
Losó Adam

Reputation: 548

negative number input(EditText) getting unsigned

i'm doing a cable length calculator, and i'm having trouble with negative numbers.

EditTexts are like this

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

Then i use them like this:

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

After i do a simple calculation:

double OPBmax = TXmax - RXmax;

Somewhere the inputted negative number turns positive. i'm guessing at the toString conversation but i don't find anything on how to prevent this.

Upvotes: 1

Views: 6657

Answers (2)

Vito Gentile
Vito Gentile

Reputation: 14366

I've tried this, and it works...

Activity:

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>

If I type -2, after clicking the TextView displays -2.0

If you're using Eclipse, try Project --> Clean..., and select your project. Maybe this will help.

Upvotes: 0

gianguyen
gianguyen

Reputation: 221

Use 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" />

Upvotes: 6

Related Questions