我正在尝试为ExpenseName和ExpenseCost Edittext创建一个验证,这样,如果没有用户输入,它将提醒用户输入一些数据,并且布尔值中的“文本”也有错误,原因是颜色为红色。我从另一个.java复制了代码,以确保可以正常工作
package com.example.back4app.userregistrationexample.Classes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.back4app.userregistrationexample.R;
public class ExpenseActivity extends AppCompatActivity {
private EditText ExpenseName;
private EditText ExpenseCost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expense);
ExpenseName = (EditText) findViewById(R.id.txt_expensename);
ExpenseCost = (EditText) findViewById(R.id.txt_expensecost);
final Button btn_addexpense = findViewById(R.id.btn_addexpense);
btn_addexpense.setOnClickListener(new View.OnClickListener()); {
@Override
public void onClick(View v) {
//Validating the log in data
boolean validationError = false;
StringBuilder validationErrorMessage = new StringBuilder("Please, insert ");
if (isEmpty(ExpenseCost)) {
validationError = true;
validationErrorMessage.append("the cost of the expense");
}
if (isEmpty(ExpenseName)) {
if (validationError) {
validationErrorMessage.append(" and ");
}
validationError = true;
validationErrorMessage.append("the name of the expense");
}
}
}
private boolean isEmpty(EditText text) {
if (text.getText().toString().trim().length() > 0) {
return false;
} else {
return true;
}
}
}
}
我尝试将'('和')'放在“ text”之外,但是它仍然是红色,并且我还尝试将“ isEmpty”更改为任何其他名称,因为它可能是在另一个.java上读取,但仍然同样的事情发生。此外,我试图重建项目和清理项目,但没有任何效果,也试图使缓存无效/重新启动,原因是我读到有人说android出现这样的bug并不少见。我的代码有问题吗?
编辑:
好的。因此我关闭了OnClickListener,但现在出现了更多“错误:';”预期”出现,并且“ OnClickListener'是抽象的;无法实例化”
答案 0 :(得分:0)
我很确定这是因为您的.setOnClickListener
未关闭。
final Button btn_addexpense = findViewById(R.id.btn_addexpense);
btn_addexpense.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Validating the log in data
boolean validationError = false;
StringBuilder validationErrorMessage = new StringBuilder("Please, insert ");
if (isEmpty(ExpenseCost)) {
validationError = true;
validationErrorMessage.append("the cost of the expense");
}
if (isEmpty(ExpenseName)) {
if (validationError) {
validationErrorMessage.append(" and ");
}
validationError = true;
validationErrorMessage.append("the name of the expense");
}
}
}); //add a parentheses and semicolon to close the onClickListener
答案 1 :(得分:0)
您没有在您发布的代码中关闭onCreate
或setOnClickListener
。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expense);
ExpenseName = (EditText) findViewById(R.id.txt_expensename);
ExpenseCost = (EditText) findViewById(R.id.txt_expensecost);
final Button btn_addexpense = findViewById(R.id.btn_addexpense);
btn_addexpense.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Validating the log in data
boolean validationError = false;
StringBuilder validationErrorMessage = new StringBuilder("Please, insert ");
if (isEmpty(ExpenseCost)) {
validationError = true;
validationErrorMessage.append("the cost of the expense");
}
if (isEmpty(ExpenseName)) {
if (validationError) {
validationErrorMessage.append(" and ");
}
validationError = true;
validationErrorMessage.append("the name of the expense");
}
}
}); // here
}
private boolean isEmpty(EditText text) {
if (text.getText().toString().trim().length() > 0) {
return false;
} else {
return true;
}
}