首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android Multiple Toast

Android Multiple Toast
EN

Stack Overflow用户
提问于 2016-03-16 05:20:53
回答 2查看 119关注 0票数 0

我希望在一个onClick函数中存在多个toast,这个函数是我在xml中定义的。

代码语言:javascript
运行
复制
public void confirm(View v) {
    Name = text1.getText().toString();
    age = text2.getText().toString();
    phonenumber = text3.getText().toString();
    gender="";

    if(r1.isChecked())
        gender="MALE";
    else if(r2.isChecked())
        gender="FEMALE";
    else
        Toast.makeText(getApplicationContext(),"Select Gender",Toast.LENGTH_LONG).show();
    if (Name.length()==0)
        Toast.makeText(getApplicationContext(),"Name Is A Mandatory Field",Toast.LENGTH_LONG).show();
    if(age.length()>3 || age.length()==0)
        Toast.makeText(getApplicationContext(),"Enter Correct Age",Toast.LENGTH_LONG).show();
    if(age.length()>0 && Name.length()>0 && gender.length()>0 && phonenumber.length()>0)
        Toast.makeText(getApplicationContext(), "OTP Sent", Toast.LENGTH_LONG).show();

    }
}

这似乎不是working.Only性别工作,.Others根本不工作。

EN

回答 2

Stack Overflow用户

发布于 2016-03-16 17:31:19

这将捕获最先出现的任何错误。我建议您在UI中按照输入字段的顺序对它们进行排序。

代码语言:javascript
运行
复制
if(!r1.isChecked && !r2.isChecked)
    Toast.makeText(getApplicationContext(),"Select Gender",Toast.LENGTH_LONG).show();
else if (Name.length()==0)
    Toast.makeText(getApplicationContext(),"Name Is A Mandatory Field",Toast.LENGTH_LONG).show();
else if(age.length()>3 || age.length()==0)
    Toast.makeText(getApplicationContext(),"Enter Correct Age",Toast.LENGTH_LONG).show();
else if(age.length()>0 && Name.length()>0 && gender.length()>0 && phonenumber.length()>0)
    Toast.makeText(getApplicationContext(), "OTP Sent", Toast.LENGTH_LONG).show();
else if(r1.isChecked()) 
    gender="MALE";
else if(r2.isChecked())
    gender="FEMALE";

这不是验证用户输入的理想方法。让多个祝酒词一个接一个地出现并不是一个好主意。在一个网站中,如果用户错过了几个字段,你可以有一个自定义的通知,告诉你,请输入所有字段,或者只是发布第一个字段丢失的通知,然后当完成后,下一个缺少的字段通知将显示出来。

票数 0
EN

Stack Overflow用户

发布于 2016-03-16 05:33:54

使用if (Name.trim().equals(""))代替if (Name.length==0)

代码语言:javascript
运行
复制
public void confirm(View v) {
    Name = text1.getText().toString();
    age = text2.getText().toString();
    phonenumber = text3.getText().toString();
    gender="";

    if(r1.isChecked())
        gender="MALE";
    else if(r2.isChecked())
        gender="FEMALE";
    else
        Toast.makeText(getApplicationContext(),"Select Gender",Toast.LENGTH_LONG).show();
    **if (Name.trim().equals(""))**
        Toast.makeText(getApplicationContext(),"Name Is A Mandatory Field",Toast.LENGTH_LONG).show();
    **if(age.length()>3 || age.trim().equals(""))**
        Toast.makeText(getApplicationContext(),"Enter Correct Age",Toast.LENGTH_LONG).show();
    if(age.length()>0 && Name.length()>0 && gender.length()>0 && phonenumber.length()>0)
        Toast.makeText(getApplicationContext(), "OTP Sent", Toast.LENGTH_LONG).show();
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36022403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档