2017-04-20 9 views
8

알리미를 자주 자주하지는 않지만 할 때마다 documentation을 읽고 그 방법을 이해해야합니다. 저는 지금이 일을 몇 번해야만하기 때문에, 저는 앞으로 다시 올 수있는 답을 쓰려고합니다. 특히 나는 (확인 및 취소)하나, 둘, 셋 버튼이있는 Android 알리미 대화 상자

  • 하나의 버튼 (OK)
  • 두 개의 버튼에 대한 기본 코드를 비교
  • 세 개의 버튼

그것은 것 (음, 다른 긍정적 인) 할 미래에 쉽게 참조하고 수정할 수 있도록이 세 가지 공통 경고 유형에 대한 기본 코드를 한 곳에서 제공하는 것이 좋습니다. This question은 하나의 버튼에 대해이를 수행하는 방법을 묻습니다.

나는 내 대답을 아래에 추가 할 예정입니다.

답변

29

하나 개의 버튼

enter image description here

import android.support.v7.app.AlertDialog; 

public class MainActivity extends AppCompatActivity { 

    public void showAlertDialogButtonClicked(View view) { 

     // setup the alert builder 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("My title"); 
     builder.setMessage("This is my message."); 

     // add a button 
     builder.setPositiveButton("OK", null); 

     // create and show the alert dialog 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 
    } 
} 

두 개의 버튼

enter image description here

public class MainActivity extends AppCompatActivity { 

    public void showAlertDialogButtonClicked(View view) { 

     // setup the alert builder 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("AlertDialog"); 
     builder.setMessage("Would you like to continue learning how to use Android alerts?"); 

     // add the buttons 
     builder.setPositiveButton("Continue", null); 
     builder.setNegativeButton("Cancel", null); 

     // create and show the alert dialog 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 
    } 
} 

세 개의 버튼

0 너무 오래 모든 수평에 맞게 버튼 텍스트, 그것은 자동으로 세 개의 버튼의 수직 열에 배치받을 경우 1,238,945,985,134,235,184,473,210

public class MainActivity extends AppCompatActivity { 

    public void showAlertDialogButtonClicked(View view) { 

     // setup the alert builder 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Notice"); 
     builder.setMessage("Launching this missile will destroy the entire universe. Is this what you intended to do?"); 

     // add the buttons 
     builder.setPositiveButton("Launch missile", null); 
     builder.setNeutralButton("Remind me later", null); 
     builder.setNegativeButton("Cancel", null); 

     // create and show the alert dialog 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 
    } 
} 

.

enter image description here

취급 버튼

OnClickListener 위의 예에서 null이었다 클릭합니다. null을 청취자로 대체하여 사용자가 버튼을 누르면 무언가를 할 수 있습니다. 예를 들어 :

builder.setPositiveButton("Launch missile", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 

     // do something like... 
     launchMissile(); 
    } 
}); 

에가는 당신이 할 수있는 대화 상자의 더 많은 종류가 있습니다. 이에 대한 도움말은 documentation을 참조하십시오.

AlertDialog에는 3 개의 버튼 만 지원되므로 다음은 목록이있는 대화 상자의 예입니다.

public class MainActivity extends AppCompatActivity { 

    public void showAlertDialogButtonClicked(View view) { 

     // setup the alert builder 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Choose an animal"); 

     // add a list 
     String[] animals = {"horse", "cow", "camel", "sheep", "goat"}; 
     builder.setItems(animals, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       switch (which) { 
        case 0: // horse 
        case 1: // cow 
        case 2: // camel 
        case 3: // sheep 
        case 4: // goat 
       } 
      } 
     }); 

     // create and show the alert dialog 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 
    } 
} 

enter image description here

는 라디오 버튼리스트와 체크 박스리스트의 유사한 예는 this answer를 참조하십시오.

노트

  • 를 사용하여 문자열 리소스보다는 하드 코딩 된 문자열.
  • 대화 상자를 쉽게 다시 사용할 수 있도록 DialogFragment을 확장하는 클래스의 모든 것을 래핑 할 수 있습니다. (도움 this을 참조하십시오.)
  • 이러한 예는 API 11에 이전 버전을 지원하기 위해 지원 라이브러리를 사용 그래서 가져 오기가

    import android.support.v7.app.AlertDialog; 
    
  • 해야 나는 간결 위의 예에서 onCreate 방법을 생략. 거기에 특별한 것은 없었습니다.

+0

보기? –

+0

@Eduardo Lion, 나는 당신이'showAlertDialogButtonClicked (View view)'의'view'를 언급한다고 가정합니다. 이것은 버튼의'onClick()'메소드에서의 Button입니다. 당신은 그것을 무시할 수 있습니다. 대화 상자에서 전혀 사용하지 않습니다. 이 대화 상자는 Context를 필요로하는데,이 경우에는 'this', 즉 Activity가 필요합니다. – Suragch