Java Android EditText具有固定的输入
我程序的这一部分应该是一个用户输入字段,用于输入要购买的物品的数量。我无法做到的是,当程序最终在emulator上运行时,我更改了EditText中的textfield编号(在我的情况下名为“etxtQuantity”),我使用的编号将被忽略,并恢复为使用默认条目。另外,如果我的语句也不能像我预期的那样工作,那么它会接受所有内容Java Android EditText具有固定的输入,java,android,input,android-edittext,Java,Android,Input,Android Edittext,我程序的这一部分应该是一个用户输入字段,用于输入要购买的物品的数量。我无法做到的是,当程序最终在emulator上运行时,我更改了EditText中的textfield编号(在我的情况下名为“etxtQuantity”),我使用的编号将被忽略,并恢复为使用默认条目。另外,如果我的语句也不能像我预期的那样工作,那么它会接受所有内容 private void setupAddRecordButton() { Button button = (Button) findViewById(R.i
private void setupAddRecordButton() {
Button button = (Button) findViewById(R.id.btnAddCart);
button.setOnClickListener(new View.OnClickListener() {
final TextView countText = (TextView) findViewById(R.id.txtAddCartCount);
EditText quantity = (EditText) findViewById(R.id.etxtQuantity);
String quantity2 = quantity.getText().toString();
final TextView text2 = (TextView) findViewById(R.id.txtQuantityCheck);
@Override
public void onClick(View v) {
if (! quantity2.equals(".") || quantity2.equals("") || quantity2.equals("-")) {
count++;
long newId = myDb.insertRow("GTX 950", 140, quantity2);
myDb.getRow(newId);
countText.setText("Successfully added " + count + " Times");
}
else
{
text2.setText("Quantity needed!");
}
}
});
}
请把电话移到这一行
stringquantity2=quantity.getText().toString()代码>
在onClick()中尝试使用
quantity .addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
quantity2=charSequence.toString();
if (! quantity2.equals(".") || quantity2.equals("") || quantity2.equals("-")) {
count++;
long newId = myDb.insertRow("GTX 950", 140, quantity2);
myDb.getRow(newId);
countText.setText("Successfully added " + count + " Times");
}
else
{
text2.setText("Quantity needed!");
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
在活动的onCreate()回调函数中初始化编辑文本,并使用字符串quantity2=quantity.getText().toString()在按钮的onClick()功能中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText quantity = (EditText) findViewById(R.id.etxtQuantity);
Button button = (Button) findViewById(R.id.btnAddCart);
TextView text2 = (TextView) findViewById(R.id.txtQuantityCheck);
TextView countText = (TextView) findViewById(R.id.txtAddCartCount);
button.setOnClickListener(new View.OnClickListener() {
String quantity2 = quantity.getText().toString();
@Override
public void onClick(View v) {
if (! quantity2.equals(".") || quantity2.equals("") ||quantity2.equals("-")) {
count++;
long newId = myDb.insertRow("GTX 950", 140, quantity2);
myDb.getRow(newId);
countText.setText("Successfully added " + count + " Times");
}
else
{
text2.setText("Quantity needed!");
}
}
});
}
请移动EditText代码字符串quantity2=quantity.getText().toString();在onclickWow内部,速度如此之快,解决了部分问题!请接受我的回答,如果它有帮助的话。还有一件事,我的if语句接受null“”,而我认为它会因为我的条件而提示使用else?你有错误的条件,你忘了写!在每一个定量条件下,你都解决了我问题的主要部分,因此我将标记你作为答案。我在if语句中发现了语法问题,将它改为(!(quantity2.equals(“.”| | quantity2.equals(““”| | quantity2.equals(“-”)))是的,我也注意到了,但当我跳过它时,我认为你需要这样的条件,原因是什么?它使用更少的资源吗?我这样做是因为我发现模块化代码更容易阅读。“在我的onCreate上有大量的变量看起来并不那么令人愉快。@DeanNewb这不是资源问题。我建议onCreate,因为最好在活动可见之前初始化UI控件。我的意思是onCreate就是为了实现这一点,至于moudular方法,您可以在另一个函数中编写在onClick()中编写的代码,比如setupAddRecordButton(),然后在onClick()中调用该函数