2011-07-01 4 views
1

다른 대화 상자의 팝업 팝업을 포함하는 응용 프로그램을 만들었습니다. 내가 작성한 코드는 다음과 같습니다AlertDialog를 제어하는 ​​방법

지금
if (lDiffFromToday >= 0 && lDiffFromToday <= DeclareVariable.CYCLE_MAX_LENGTH) 
      { 

       AlertDialog.Builder alrtStartMonitoring = new AlertDialog.Builder(this); 
       alrtStartMonitoring.setTitle("    Start Monitoring"); 
       alrtStartMonitoring.setMessage("Set start date of cycle as"+" "+sdFormatter.format(dtSelDate)); 
       alrtStartMonitoring.setPositiveButton("Yes", this); 
       AlertDialog alert = alrtStartMonitoring.create(); 
       alert.show(); 
      } 

else if (dtSelDate.getTime()> dtStartDate.getTime() && dtSelDate.getTime() <= currentDate.getTime() && !bCycleStopped) 
     { 
      long lDiffFromStart =dtSelDate.getTime()-dtStartDate.getTime(); 
      lDiffFromStart=lDiffFromStart/(1000 * 60 * 60 * 24); 
      if (lDiffFromStart >= DeclareVariable.CYCLE_MIN_LENGTH) 
      { 
       bActionOk = true; 
       AlertDialog.Builder alrtStartMonitoring = new AlertDialog.Builder(this); 
       alrtStartMonitoring.setTitle("    Confirm New Cycle"); 
       alrtStartMonitoring.setMessage("Set start date of cycle as" + " " + sdFormatter.format(dtSelDate)); 
       alrtStartMonitoring.setPositiveButton("Yes", this); 
       AlertDialog alert = alrtStartMonitoring.create(); 
       alert.show(); 
      } 
} 


public void onClick(DialogInterface dialog, int id) 
    { 
     CycleManager.getSingletonObject().setHistoryDate(dtSelDate); 
     int iStopStartCount = CycleManager.getSingletonObject().getStopStartCount();     
     if(iStopStartCount>0) 
      CycleManager.getSingletonObject().setStopStartDate(dtSelDate, iStopStartCount); 
     displayDay(); 
    } 

제 질문은 각 대화에 대해 내가 다른 onclick 기능을 필요로하지만, 내 경우에는 내가 충돌이있을 수있는 것보다 다른 온 클릭 기능을 쓸 때이다. 각 대화 상자 안에 onclick 함수를 작성하여 문제를 해결할 수는 있지만 그 경우에는 최종 변수로 선언해야하므로 어떻게 모든 대화 상자에서 함수를 작성하여 수행 할 수 있습니까?

+0

코드를 올바르게 포맷하십시오. – mohamede1945

+0

죄송합니다. 제발 대답을 말해주십시오. – AndroidDev

답변

1

또 다른 해결책은 클래스의 AlertDialog 인스턴스를 만드는 것입니다. 그 다음의 OnClick 방법에 :

public void onClick(DialogInterface dialog, int id) 
{ 
    if (dialog == m_Dialog1) 
    { 
    // server dialog 1 
    } 

} 
+0

어떻게 할 수 있습니까? – AndroidDev

+0

이 맞습니까? [DialogInterface.OnClick()] (http://developer.android.com/reference/android/content/DialogInterface.OnClickListener.html)에서 onClick을 구현하는 경우 id는 누른 버튼 ID입니다. –

+0

예,하지만 ID는 확인하지 않고 대화 상자 자체를 확인합니다. 너 – kgiannakakis

0

나는 여러분이이 방법을 만들 수 있습니다 대신

alrtStartMonitoring.setPositiveButton("Yes", this); 

DialogInterface.OnClickListener

를 구현하는 클래스를 변경 한 것을 볼 수 있습니다

alrtStartMonitoring.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 

public void onClick (DialogInterface dialog, int which) { 

} 
}); 

그리고 각각의 setPositiveButton에 대해 서로 다른 012를 정의 할 수 있습니다 3,청취자

희망이

+0

yeh 나는 알고있다. 그러나 그렇게하는 것에 나는 필요하지 않은 나의 변수 결승전을 만들어야했다. .thats 나는이 단계를 건너 뛴다. – AndroidDev

+0

당신이 그들을 실례로서 선언하면, 당신은 그들을 가질 필요가 없을 것이다. . – mohamede1945

+0

예, 맞습니다. 두 번째 대화 상자가 뜨면 확인을 클릭하면 장치에 오류가 발생하고 응용 프로그램이 종료됩니다 .. \ – AndroidDev

0

왜 자신의 DialogInterface.OnClickListener 클래스를 만들고 마지막으로 표시하지 않으려는 메인 클래스에서 해당 변수를 인스턴스화하지만, 그들에 대한 액세스 권한을 갖고 싶어하지 않을 수 있습니다 (효과적으로 주사하십시오). 그럼 할 수있어

FooDialogOnClickListener l1 = new FooDialogOnClickListener(dtSelData, ...); 
BarDialogOnClickListener l2 = new BarDialogOnClickListener(iStopStartCount, ...); 

if (...) { 
    // ... 
    alrtStartMonitoring.setPositiveButton("Yes", l1); 
} else { 
    // ... 
    alrtStartMonitoring.setPositiveButton("Yes", l2); 
}