2014-03-19 78 views
0

否則,如果在我的代碼。基本上我有幾個條件可以開始繪製圈子之前完成。但是,在執行程序時,即使滿足條件,彈出對話框也會出現幾次。如果有消息要顯示,我只想要一個對話框出現一次。我是否正確地做?請指教。Android:如果elseif彈出對話框

我的代碼如下:

if (MainActivity.isClicked() == true) { 

    if (condition 1) { 

       canvas.drawCircle(x, y, radius, redPaint); 
       invalidate(); 

    } else if (condition 2) { 
       canvas.drawCircle(x, y, radius, bluePaint); 
       invalidate();   

    } else if (condition 3){ 
       msg = "green not available"; 

    } else { 
       msg =" please add more colors"; 
    } 
AlertDialog.Builder builder = new Builder(getContext()); 
      builder.setTitle("Warning").setMessage(msg); 
      builder.setPositiveButton("Okay",new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface di,int i) { 

           } 

     }); 

     builder.create().show(); 
    }// if 

回答

0

用於顯示警報創建一個方法,當條件滿足爲:

private void showAlert(String str_mesg){ 

AlertDialog.Builder builder = new Builder(getContext()); 
builder.setTitle("Warning").setMessage(str_mesg); 
builder.setPositiveButton("Okay",new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface di,int i) { 

      } 
    }); 
} 

呼叫showAlertif-else梯子來顯示警報:

...... 
    } else if (condition 3){ 
      msg = "green not available"; 
      showAlert(msg); 

    } else { 
     msg =" please add more colors"; 
     showAlert(msg); 
    } 
    ..... 
+0

嘿謝謝你的精神。我已經使用了這個方法,但是警報劇照多次出現.. – user3306996