开发者

how to use and edittext input as an int for a switch

开发者 https://www.devze.com 2023-04-04 10:31 出处:网络
I want to use the users input from the edit text, covert it into an int and use it in my switch statment

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开发者_StackOverflow.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 an int an Editable. display.getText() returns an Editable object. I think what you actually want is

input = Integer.parseInt(display.getText().toString());

Since 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.


display.getText() will return a String, not an int, which is what you've declared input as. You'll need to call input = Integer.parseInt(display.getText().toString()) and handle any Exceptions that might throw (I don't remember off the top of my head).

Your Thread code is correct but will never be called. You'd be better off just calling Thread.sleep(2000) wherever you need it - it doesn't need to be wrapped in a Thread (but you do still need to catch the InterruptedException). However, if you want to keep it the way it is, you'll need to call pause.start().

0

精彩评论

暂无评论...
验证码 换一张
取 消