Converting EditText to int? (Android)

前端 未结 11 1208
深忆病人
深忆病人 2020-12-09 03:08

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

@SuppressWarnin         


        
11条回答
  •  自闭症患者
    2020-12-09 03:31

    I'm very sleepy and tired right now but wouldn't this work?:

    EditText et = (EditText)findViewById(R.id.editText1);
    String sTextFromET = et.getText().toString();
    int nIntFromET = new Integer(sTextFromET).intValue();
    

    OR

    try
    {
        int nIntFromET = Integer.parseInt(sTextFromET);
    }
    catch (NumberFormatException e)
    {
        // handle the exception
    }
    

提交回复
热议问题