2011-05-10 4 views
3

두 개의 'YES'와 'NO'버튼이있는 DialogAlert를 만들고 싶습니다. 대화 상자의 결과를 부울 변수로 캡처하고 싶습니다. 사용자가 '예'버튼을 클릭하고 DialogResult가 true 값을 반환한다고 가정하고 사용자가 '아니오'버튼을 클릭하면 DialogResult는 False 값을 반환해야합니다. 이 일을 도와주세요. 미리 감사드립니다.android에서 DialogResult를 얻는 방법?

답변

3

내가에 AlertDialog (see documentation here)를 사용하는 것이 코드를

boolean result; 
AlertDialog.Builder invalid_input_dialog = new AlertDialog.Builder(Select_party_foods1.this); 
      invalid_input_dialog.setTitle("Event Organise") 
      .setMessage(dialog_message) 
      .setCancelable(true) 
      .setPositiveButton("Ok", new DialogInterface.OnClickListener(){ 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
       result=true; 
       System.out.println("The result is "+result); 
       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener(){ 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       result=false; 
       System.out.println("The result is "+result);       
      } 

      }) 
      .show(); 
+0

안녕하세요! 사실이 메서드 밖에 변수를 가져 오려고합니다. main 메소드에서 ShowAlert ("Message")를 호출하고 ShowAlert ("Message")가 선택된 버튼을 기준으로 부울 결과를 반환하도록합니다. – Prachi

4

을보십시오. DialogResult 클래스가 있다면 코드는 다음과 같습니다.

DialogResult result = new DialogResult(); 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setMessage("Are you sure you want to exit?") 
    .setCancelable(false) 
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      // User clicked Yes: set the value to your result class here. E.g: 
      MyActivity.result.setValue(true); 
     } 
    }) 
    .setNegativeButton("No", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      // User clicked No: set the value to your result class here. E.g: 
      MyActivity.result.setValue(false); 
     } 
    }); 
AlertDialog alert = builder.create(); 
+0

이 코드를 구현하려고 시도했지만 'DialogResult 클래스를 확인할 수 없습니다'오류가 발생했습니다. – Prachi

+1

DialogResult라는 클래스가 있다고 질문하셨습니다. – Kristian

관련 문제