I want to use the users input from the edit text, covert it into an int and use it in my switch statment
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputType;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ToggleButton;
public class MainScreen extends Activity implements View.OnClickListener {
Button convert;
Button erase;
EditText display;
ToggleButton switcher;
int input;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pause.start();
// Calls my variables
InitializeVars();
}
// SLEEP 2 SECONDS HERE ...
Thread pause = new Thread() {
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
// My variables
private void InitializeVars() {
// TODO Auto-generated method stub
convert = (Button) findViewById(R.id.bConvert);
erase = (Button) findViewById(R.id.bErase);
display = (EditText) findViewById(R.id.etDisplay);
display.setInputType(InputType.TYPE_CLASS_NUMBER);
switcher = (ToggleButton) findViewById(R.id.tbSwitch);
switcher.setOnClickListener(this);
convert.setOnClickListener(this);
erase.setOnClickListener(this);
}
// My functions for anything that is clickable embedded with a switch
// statement
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.tbSwitch:
if (switcher.isChecked()) {
display.setText("Text1");
} else {
display.setText("Text2");
}
break;
case R.id.bErase:
display.getText().clear();
break;
case R.id.bConvert:
input = Integer.getInteger(display.getText().toString());
input = (input / 10);
switch (input) {
case input = 10:
}
break;
default:
break;
}
}
}
So after the input this is what it looks like
and when i try to use my input in my case(for the switch statment) get “case expressions must be constant expressions”
Well, the first thing I’m noticing about that line is that you’re trying to give
input, which is anintanEditable.display.getText()returns an Editable object. I think what you actually want isSince parseInt() will throw a NumberFormatException, you might want to implement a try/catch block to catch any input that isn’t an integer. It will make your application more robust.
As for your thread. You don’t call it anywhere. It’s fine but wherever you need it, you must call
pause.start()though it’s really unnecessary. I agree with President Evil on that regard.