如何使用和编辑文本输入作为开关的 int

发布于 2024-12-04 07:15:10 字数 2305 浏览 3 评论 0原文

我想使用编辑文本中的用户输入,将其转换为 int 并在我的 switch 语句中使用它

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;
    }
}

}

所以在输入之后,这就是它的样子 当我尝试在我的情况下使用我的输入(对于 switch 语句)时,得到“情况表达式必须是常量表达式”

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒七 2024-12-11 07:15:10

嗯,关于该行,我注意到的第一件事是您试图提供 input,它是一个 intEditabledisplay.getText() 返回一个 Editable 对象。我认为您真正想要的是,

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

由于 parseInt() 将抛出 NumberFormatException,因此您可能需要实现一个 try/catch 块来捕获任何不是整数的输入。它将使您的应用程序更加健壮。

至于你的线程。你不会在任何地方调用它。这很好,但无论你在哪里需要它,你都必须调用 pause.start(),尽管这确实没有必要。在这一点上我同意邪恶总统的观点。

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.

一绘本一梦想 2024-12-11 07:15:10

display.getText() 将返回一个字符串,而不是一个 int,这就是您所声明的 input 的值。您需要调用 input = Integer.parseInt(display.getText().toString()) 并处理可能抛出的任何异常(我不记得了)。

您的 Thread 代码是正确的,但永远不会被调用。你最好在任何需要的地方调用 Thread.sleep(2000) ,它不需要包装在 Thread 中(但你仍然需要捕获 InterruptedException)。但是,如果您想保持原样,则需要调用 pause.start()

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文