2011-11-18 6 views
5

내 응용 프로그램이 웹에서 다운로드하는 데이터를 저장할 위치를 사용자가 선택할 수 있도록 응용 프로그램 시작 부분에 경고 대화 상자를 만듭니다. 지금 달성하고 싶은 것은 내부/외부 저장소의 크기에 따라 선택 항목 중 하나를 설정하려고합니다.Android가 경고 대화 상자에서 선택한 항목을 설정합니다.

@SuppressWarnings("static-access") 
public void createDialog(){ 


    final CharSequence[] items = {"Phone Memory - "+memorysize+" free space", "SD Card - "+megAvailable+" MB free space"}; 

    final int userId = rpc.getUserId(this); 
    final String servername = rpc.getCurrentServerName(this); 

    SharedPreferences stampiiSettings = PreferenceManager.getDefaultSharedPreferences(MyCollectionList.this); 
    final SharedPreferences.Editor editor = stampiiSettings.edit(); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent()); 
    builder.setTitle("Select Storage Path"); 
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 

      if(item == 0){ 

       rpc.createFoldersInInternalStorage(servername, userId, MyCollectionList.this); 
       Toast.makeText(getApplicationContext(), "Selected Storage Path : Phone Memory", Toast.LENGTH_SHORT).show(); 
       editor.putInt("storagePath", 1); 
       editor.commit(); 
      } else if (item == 1){ 

       rpc.createFoldersInExternalStorage(servername, userId, MyCollectionList.this); 
       Toast.makeText(getApplicationContext(), "Selected Storage Path : SD Card", Toast.LENGTH_SHORT).show(); 
       editor.putInt("storagePath", 2); 
       editor.commit(); 
      } 
     }}); 

     builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
       mHandlerUpdateUi.post(mUpdateUpdateUi); // update UI    
     } 
     }); 
     AlertDialog alert = builder.show(); 
} 

내가 달성하고자하는 또 다른 것은 나는 그가 어떤 항목을 선택하지 않은 경우 경고 대화 상자를 닫습니다 사용자를 방지 할 수있는 방법 : 여기에 내가 대화 상자를 만드는 데 사용하고 코드입니다. 뒤로 버튼을 누르거나 확인 버튼을 클릭 할 때 대화 상자를 닫고 싶지 않습니다. 모든 아이디어/제안/도움을 환영합니다!

답변

17

당신이 대화 상자의 확인 버튼을 클릭 할 때의 값을 얻을 수 있습니다 :

int selected = 0; // or whatever you want 
builder.setSingleChoiceItems(items, selected, new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int item) { 
       //onclick 
    }}); 
3

버튼을 닫는 당신은 당신이 지역과 스피너이 클래스에 대해 정의 할 수 있습니다

선택한 항목에 대한
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // no need to write anything here just implement this interface into this button 
     } 
}); 

이 방법처럼 확인과 취소 버튼 또는 당신이 어떤 변수에이 값을 할당 할 수 있습니다 정의 할 수 있습니다 좋아하는 것.

int selected = 0; // if you want in integer or 
String selected = "internal"; // you can go with string 

지금 당신은이에 기본 값으로 설정하고이 같은 수행

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
      // here get the selected item value 
    } 
}); 
관련 문제