Converting EditText to int? (Android)

2019-01-17 22:31发布

I am wondering how to convert an edittext input to an int, I have the user input a number, it than divides it by 8.

MainActivity.java

@SuppressWarnings("unused")
public void calcSpeed(View view)
{       
    setContentView(R.layout.activity_speed);     

    final TextView mTextView = (TextView) findViewById(R.id.textView3);
    mTextView.setText("You should be getting: " +netSpedCalcd);
}

activity_main.xml

    <EditText
    android:id="@+id/editText1" 
    android:inputType="number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="62dp"
    android:ems="10" >

9条回答
倾城 Initia
2楼-- · 2019-01-17 22:47

I had the same problem myself. I'm not sure if you got it to work though, but what I had to was:

EditText cypherInput;
cypherInput = (EditText)findViewById(R.id.input_cipherValue);
int cypher = Integer.parseInt(cypherInput.getText().toString());

The third line of code caused the app to crash without using the .getText() before the .toString().

Just for reference, here is my XML:

<EditText
    android:id="@+id/input_cipherValue"
    android:inputType="number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
查看更多
We Are One
3楼-- · 2019-01-17 22:53

Try the line below to convert editText to integer.

int intVal = Integer.parseInt(mEtValue.getText().toString());
查看更多
来,给爷笑一个
4楼-- · 2019-01-17 22:59

Try this,

EditText x = (EditText) findViewById(R.id.editText1);
int n = Integer.parseInt(x.getText().toString());
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-17 23:00
int total_Parson = Integer.parseInt(etRegularTickets.getText().toString());
int ticket_price=Integer.parseInt(TicketData.get(0).getTicket_price_regular());
total_ticket_amount = ticket_price * total_Parson;
etRegularPrice.setText(""+total_ticket_amount);
查看更多
三岁会撩人
6楼-- · 2019-01-17 23:00

You can use parseInt with try and catch block

try
{
    int myVal= Integer.parseInt(mTextView.getText().toString());
}
catch (NumberFormatException e)
{
    // handle the exception
    int myVal=0;
}

Or you can create your own tryParse method :

public Integer tryParse(Object obj) {
    Integer retVal;
    try {
        retVal = Integer.parseInt((String) obj);
    } catch (NumberFormatException nfe) {
        retVal = 0; // or null if that is your preference
    }
    return retVal;
}

and use it in your code like:

int myVal= tryParse(mTextView.getText().toString());

Note: The following code without try/catch will throw an exception

int myVal= new Integer(mTextView.getText().toString()).intValue();

Or

int myVal= Integer.decode(mTextView.getText().toString()).intValue();
查看更多
孤傲高冷的网名
7楼-- · 2019-01-17 23:01

You can use like this

 EditText dollar=(EditText) findViewById(R.id.money);
 int rupees=Integer.parseInt( dollar.getText().toString());
查看更多
登录 后发表回答