Java 用户输入验证获胜';不行!如果我点击submit(提交),即使进入窗口为空,仍然会添加| Android开发
我试图允许用户将自己添加到数据库中,但即使在没有输入的情况下,它仍然允许用户添加,有人能帮忙吗 我尝试过添加用户输入验证,但它似乎不起作用。这个问题可能很简单,但我只是一个初学者,没有什么经验,任何输入都会有帮助,谢谢Java 用户输入验证获胜';不行!如果我点击submit(提交),即使进入窗口为空,仍然会添加| Android开发,java,android,eclipse,import,Java,Android,Eclipse,Import,我试图允许用户将自己添加到数据库中,但即使在没有输入的情况下,它仍然允许用户添加,有人能帮忙吗 我尝试过添加用户输入验证,但它似乎不起作用。这个问题可能很简单,但我只是一个初学者,没有什么经验,任何输入都会有帮助,谢谢 public class MainActivity extends AppCompatActivity { private Button button; private EditText IdText; private EditText NameTex
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText IdText;
private EditText NameText;
private EditText AgeText;
private EditText WeightText;
private EditText HeightText;
private EditText ReachText;
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IdText = (EditText) findViewById(R.id.IdText);
NameText = (EditText) findViewById(R.id.NameText);
HeightText = (EditText) findViewById(R.id.HeightText);
AgeText = (EditText) findViewById(R.id.AgeText);
WeightText = (EditText) findViewById(R.id.WeightText);
ReachText = (EditText) findViewById(R.id.ReachText);
button = (Button) findViewById(R.id.button);
dbHandler = new MyDBHandler(this);
AddData();
}
public void AddData() {
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = dbHandler.insertData(IdText.getText().toString(),
NameText.getText().toString(),
AgeText.getText().toString(),
HeightText.getText().toString(),
WeightText.getText().toString(),
ReachText.getText().toString());
if (isInserted == true)
Toast.makeText(MainActivity.this, "Fighter added", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data not inserted", Toast.LENGTH_LONG).show();
if ((IdText.getText().toString()).isEmpty())
{
IdText.setError("Please fill out your ID");
}
else if ((NameText.getText().toString()).isEmpty())
{
NameText.setError("Please fill out your Name");
}
else if ((AgeText.getText().toString()).isEmpty())
{
AgeText.setError("Please fill out your Age");
}
else if ((HeightText.getText().toString()).isEmpty())
{
HeightText.setError("Please fill out your Height in centimeters");
}
else if ((WeightText.getText().toString()).isEmpty())
{
WeightText.setError("Please fill out your weight in kilos");
}
else if ((ReachText.getText().toString()).isEmpty())
{
ReachText.setError("Please fill out your reach in inches");
}
}
}
);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
在添加项目之前,在开始处添加您的支票 同时在条件中添加
return
关键字,这样下面的代码就不会被执行
使用以下代码段:
@Override
public void onClick(View v) {
if ((IdText.getText().toString()).isEmpty())
{
IdText.setError("Please fill out your ID");
return;
}
else if ((NameText.getText().toString()).isEmpty())
{
NameText.setError("Please fill out your Name");
return;
}
else if ((AgeText.getText().toString()).isEmpty())
{
AgeText.setError("Please fill out your Age");
return;
}
else if ((HeightText.getText().toString()).isEmpty())
{
HeightText.setError("Please fill out your Height in centimeters");
return;
}
else if ((WeightText.getText().toString()).isEmpty())
{
WeightText.setError("Please fill out your weight in kilos");
return;
}
else if ((ReachText.getText().toString()).isEmpty())
{
ReachText.setError("Please fill out your reach in inches");
return;
}
else{
boolean isInserted = dbHandler.insertData(IdText.getText().toString(),
NameText.getText().toString(),
AgeText.getText().toString(),
HeightText.getText().toString(),
WeightText.getText().toString(),
ReachText.getText().toString());
if (isInserted == true)
Toast.makeText(MainActivity.this, "Fighter added", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data not inserted", Toast.LENGTH_LONG).show();
}
}
不客气……如果答案有帮助的话,别忘了把它标为正确的。。