2011-11-18 39 views
5

Tôi đang tạo một Hộp thoại cảnh báo ở đầu ứng dụng để cho phép người dùng chọn nơi lưu trữ dữ liệu mà ứng dụng của tôi tải xuống từ web. Thứ mà tôi muốn đạt được bây giờ phụ thuộc vào kích thước của Internal/External Storage tôi muốn thiết lập một trong các mục. Đây là mã mà tôi đang sử dụng để tạo hộp thoại:Android đặt mục đã chọn trong hộp thoại cảnh báo

@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(); 
} 

Và một điều khác mà tôi muốn đạt được, làm cách nào để ngăn người dùng đóng Hộp thoại cảnh báo nếu không chọn bất kỳ mục nào. Tôi không muốn đóng hộp thoại khi nhấn nút quay lại hoặc khi bạn nhấp vào nút OK. Bất kỳ ý tưởng/gợi ý/trợ giúp được hoan nghênh!

Trả lời

17

Làm như thế này:

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

cho nút đóng cửa, bạn có thể xác định các nút hủy với ok như cách này

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 
     } 
}); 

cho mặt hàng được lựa chọn bạn có thể làm cho spinner như địa phương xác định cho lớp này hoặc bạn có thể gán giá trị này cho bất kỳ biến như đã chọn.

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

bây giờ bạn có thể thiết lập các giá trị mặc định này và nhận được giá trị trong khi bạn click vào nút ok của hộp thoại

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
      // here get the selected item value 
    } 
}); 
Các vấn đề liên quan