2014-11-25 59 views
1

沒有得到任何錯誤,我只是難住這個friggin if語句。我希望if語句基本上說明如果未選中複選框,並且3個EditTexts爲空,則打印Toast。否則,不要打印吐司並繼續下一個活動。首先,我將複選框的值轉換爲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個輸入文本留空顯示吐司並且不繼續下一個活動。

如果用戶確認複選框,則不顯示吐司並繼續下一個活動。

+0

嘗試更改Toast版本中的此值:Toast.LENGTH_LONG toast.LENGTH_SHORT。 – 2014-11-25 04:38:09

+0

你的意思是你至少需要1個編輯文本的值? – Deepzz 2014-11-25 04:38:13

+0

爲什麼第一個條件是檢查OR條件和第二個條件AND.day1_inst如何在代碼中聲明這些變量。 – sri 2014-11-25 04:40:03

回答

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); 
    } 
+0

多數民衆贊成在仍然顯示烤麪包時,我打按鈕 – dtrodriguez 2014-11-25 04:55:46

+0

是烤麪包始終可見?和'Toast.LENGTH_LONG'需要一些時間才能看到5-10分鐘。 – 2014-11-25 04:58:41

+0

@dtrodriguez歡呼... :) – 2014-11-25 05:42:39

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"; 
       } 
      } 
     }); 
1

這種替換getApplicationContext()如果你在活動。 getActivity()如果​​你在片段。

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(""))) 
{ 

} 
2

嘗試通過字符串值的length()檢查EditText值。其次把&&運營商在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"; 
} 
3

如果條件爲PLUS,則需要使用if - else塊而不是2,如果塊

//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 
} 

希望這會有所幫助。

1

就想通了感謝米每秒Gadag =)

基本上我把if語句,把startActivity if語句在上面。然後我添加了一個變量來顯示該語句已被執行。然後我使用該變量來啓動另一個if語句來決定是否顯示敬酒。

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(); 
    } 
} 
+0

歡迎n如果我的答案幫助你加入或接受它作爲朋友.. :) – 2014-11-25 05:09:07