用户输入验证不起作用!如果我点击提交,即使输入位置为空,仍然会添加 |安卓开发

Posted

技术标签:

【中文标题】用户输入验证不起作用!如果我点击提交,即使输入位置为空,仍然会添加 |安卓开发【英文标题】:User input validation won't work! Still adds if I click submit even when entry slots are blank | Android development 【发布时间】:2017-09-22 22:37:04 【问题描述】:

我正在尝试允许用户将自己添加到数据库中,但即使没有输入,它也会继续让他们添加,有人可以帮忙吗?

我已尝试添加用户输入验证,但它似乎不起作用。问题可能很简单,但我只是一个经验不足的初学者开发人员,任何输入都会有所帮助,谢谢。

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;
    

【问题讨论】:

【参考方案1】:

在添加项目之前在开头添加您的检查

还要在条件中添加return关键字,这样下面的代码就不会被执行。

使用这个代码sn-p:

 @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();
            
    

【讨论】:

不客气......如果有帮助,别忘了将答案标记为正确......

以上是关于用户输入验证不起作用!如果我点击提交,即使输入位置为空,仍然会添加 |安卓开发的主要内容,如果未能解决你的问题,请参考以下文章

文本区域的验证不起作用

Bootstrap 4表单验证在使用无效反馈时不起作用,即使输入'is-valid'

Hololens 商店提交输入不起作用

即使输入文本有效(Javascript或Jquery),如何强制输入文本无效

验证输入字段后如何提交表单

jquery验证动态表单输入的插件不起作用