如果声明要显示吐司或继续下一个活动

时间:2014-11-25 04:29:19

标签: java android

没有任何错误我只是在这个friggin if语句上难倒。我希望if语句基本上说是否未选中复选框并且3个EditTexts为空,然后打印吐司。否则不要打印吐司并继续下一个活动。首先,我将复选框的值转换为t / f为true / false,然后执行我的if语句,如下所示。

CheckBox noPtD1 = (CheckBox) findViewById(R.id.noPTD1);
String noPt_D1 = "f";
if (noPtD1.isChecked()) {
    noPt_D1 = "t";
}



if (noPt_D1.equals("f") && day1_inst.equals("") || day1_uniform.equals("") ||
day1_location.equals(""))
{
    Toast.makeText(getApplicationContext(), "Please Enter All Data Or Select the NO PT THIS DAY
    checkbox!", Toast.LENGTH_LONG).show();
}
if(noPt_D1.equals("t") || !day1_inst.equals("") && !day1_uniform.equals("") &&
!day1_location.equals(""))
{
    //PASS VARIABLES WITH INTENT
    Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);

    //PASS VARIABLES FOR DAY 1
    intent.putExtra("noPt_D1", noPt_D1);
    intent.putExtra("day1_inst", day1_inst);
    intent.putExtra("day1_uniform", day1_uniform);
    intent.putExtra("day1_location", day1_location);
    intent.putExtra("d1hours", d1hours);
    intent.putExtra("d1min", d1min);
    intent.putExtra("d1x1", d1x1);
    intent.putExtra("d1x2", d1x2);
    intent.putExtra("d1x3", d1x3);
    intent.putExtra("d1x4", d1x4);              
    startActivity(intent);
}

这是有效的,除了那时我检查复选框,然后用一个按钮开始下一个活动,下一个活动就像它应该弹出一样,但是吐司仍然出现。我搞砸了什么?

我知道了,我在下面发布了我的答案。但要改正一下我的意图:

如果用户没有勾选复选框,并且所有3个输入文件都空白,则显示吐司,不要继续下一个活动。

如果用户选中复选框,则DONT显示吐司并继续下一个活动。

7 个答案:

答案 0 :(得分:3)

在if语句

中使用括号
if (noPt_D1.equals("f") && (day1_inst.equals("") || day1_uniform.equals("") ||
day1_location.equals("")))
{

}
if(noPt_D1.equals("t") || (!day1_inst.equals("") && !day1_uniform.equals("") &&
!day1_location.equals("")))
{

}

答案 1 :(得分:3)

你在条件 PLUS 中犯了小错误你需要使用if - else block而不是2 if blocks

//surround all && conditions inside separate braces
if (noPt_D1.equals("f") && (day1_inst.equals("") || day1_uniform.equals("") ||
day1_location.equals("")))
{
    //show toast
}
else if(noPt_D1.equals("t") || (!day1_inst.equals("") && !day1_uniform.equals("") &&
!day1_location.equals("")))
{
    //start activity
}

希望这有帮助。

答案 2 :(得分:2)

也试试这个。最好避免为你的班级名字包含特殊字符

if ("f".equals(noPt_D1) && "".equals(day1_inst) || "".equals(day1_uniform) ||
    "".equals(day1_location))
    {
        Toast.makeText(getApplicationContext(), "Please Enter All Data Or Select the NO PT THIS DAY
        checkbox!", Toast.LENGTH_LONG).show();
    }
    if("t".equals(noPt_D1) || !"".equals(day1_inst) && !"".equals(day1_uniform) &&
    !"".equals(day1_location))
    {
        //PASS VARIABLES WITH INTENT
        Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);

        //PASS VARIABLES FOR DAY 1
        intent.putExtra("noPt_D1", noPt_D1);
        intent.putExtra("day1_inst", day1_inst);
        intent.putExtra("day1_uniform", day1_uniform);
        intent.putExtra("day1_location", day1_location);
        intent.putExtra("d1hours", d1hours);
        intent.putExtra("d1min", d1min);
        intent.putExtra("d1x1", d1x1);
        intent.putExtra("d1x2", d1x2);
        intent.putExtra("d1x3", d1x3);
        intent.putExtra("d1x4", d1x4);              
        startActivity(intent);
    }

答案 3 :(得分:2)

尝试通过EditText字符串值检查length()值。其次将&&运算符放在if语句中,因为您在问题中说过:if a check box is not checked and 3 EditTexts are empty then print a toast. Otherwise dont print the toast and continue to the next activity.

if (noPt_D1.equals("f") && day1_inst.trim().length() == 0 && day1_uniform.trim().length() == 0 &&
                          day1_location.trim().length() == 0)
{
    Toast.makeText(getApplicationContext(), "Please Enter All Data", Toast.LENGTH_LONG).show();
}
else
{
     Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);

     intent.putExtra("noPt_D1", noPt_D1);
     intent.putExtra("day1_inst", day1_inst);
     intent.putExtra("day1_uniform", day1_uniform);
     intent.putExtra("day1_location", day1_location);
     intent.putExtra("d1hours", d1hours);
     intent.putExtra("d1min", d1min);
     intent.putExtra("d1x1", d1x1);
     intent.putExtra("d1x2", d1x2);
     intent.putExtra("d1x3", d1x3);
     intent.putExtra("d1x4", d1x4);              
     startActivity(intent);
}

修改

确保你应该声明我假设的字符串:

String day1_inst = your_editText_inst.getText().toString();
String day1_uniform = your_editText_uniform.getText().toString();
String day1_location = your_editText_location.getText().toString();
String noPt_D1 = "f";
if(your_check_box.isChecked())
{
    noPt_D1 = "t"; 
}
else
{
    noPt_D1 = "f"; 
}

答案 4 :(得分:1)

尝试使用OnCheckedChangeListener侦听器,如下所示:

CheckBox noPtD1 = (CheckBox) findViewById(R.id.noPTD1);
String noPt_D1 = "f";
noPtD1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    noPt_D1 = "t";
                } else {
                    noPt_D1 = "f";
                }
            }
        });

答案 5 :(得分:1)

如果您处于活动状态,请将getApplicationContext()替换为此项。 getActivity(),如果你是片段。

答案 6 :(得分:1)

感谢M S Gadag =)

基本上我接受了if语句并将startActivity if语句放在最上面。然后我添加了一个变量来表明该语句已被执行。然后我使用该变量启动另一个if语句来决定是否显示toast。

int showToast = 0;

if("t".equals(noPt_D1) || !"".equals(day1_inst) && !"".equals(day1_uniform) &&
!"".equals(day1_location))
{
    //PASS VARIABLES WITH INTENT
    Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);

    //PASS VARIABLES FOR DAY 1
    intent.putExtra("noPt_D1", noPt_D1);
    intent.putExtra("day1_inst", day1_inst);
    intent.putExtra("day1_uniform", day1_uniform);
    intent.putExtra("day1_location", day1_location);
    intent.putExtra("d1hours", d1hours);
    intent.putExtra("d1min", d1min);
    intent.putExtra("d1x1", d1x1);
    intent.putExtra("d1x2", d1x2);
    intent.putExtra("d1x3", d1x3);
    intent.putExtra("d1x4", d1x4);              
    startActivity(intent);

    showToast = 1;
}       

if ("f".equals(noPt_D1) && "".equals(day1_inst) || "".equals(day1_uniform) ||
"".equals(day1_location))
{
    if (showToast !=1)
    {
        Toast.makeText(getApplicationContext(), "Please Enter All Data Or Select the NO PT THIS
        DAY checkbox!", Toast.LENGTH_LONG).show();
    }
}