I have the following app that has a textview and an edittext. When the value of the edittext changes, it updates a textview’s content. Now I want the value to be received as an integer. this is the current app:
package your.test.two;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class TesttwoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText input = (EditText)findViewById(R.id.editText1);
input.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s){
update();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
}
public void update(){
EditText source = (EditText)findViewById(R.id.editText1);
Editable sourceData = source.getText();
TextView destination = (TextView)findViewById(R.id.textView1);
destination.setText(sourceData);
}
}
Now I want the sourceData editable to be somehow converted to an integer so that I can do a if statement on it as follows:
if(sourceData>255){
sourceData = 0;
}
I already changed the values of the edittext in main.xml by adding the following lines to editText1:
android:inputType="number"
android:maxLength="3"
android:numeric="integer"
Could someone give me a few guidelines as to how this can be done please. Been googling it but all I end up with is things like:
Integer i = Integer.parseInt(edittext.gettext().tostring());
&
Integer i = Integer.valueOf(String s);
And I don’t quite get those to work or understand it. Please help!? 🙁
You should be able to use
By replacing i with the variable you want to set the integer to, in your case “sourceData”, and by replacing edittext with the name of your edittext box, so “source”
So you’d get something like this: