如何使用和编辑文本输入作为开关的 int
我想使用编辑文本中的用户输入,将其转换为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,关于该行,我注意到的第一件事是您试图提供
input
,它是一个int
和Editable
。display.getText()
返回一个 Editable 对象。我认为您真正想要的是,由于 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 anint
anEditable
.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.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 declaredinput
as. You'll need to callinput = 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 callingThread.sleep(2000)
wherever you need it - it doesn't need to be wrapped in aThread
(but you do still need to catch theInterruptedException
). However, if you want to keep it the way it is, you'll need to callpause.start()
.