2012-12-13 119 views
9

我想創建一種方式用戶可以選擇以下自定義對話框與setMultiChoiceItems

enter image description here

像圖像選項眼下正在做以下

public static class CategoriesDialogFragment extends SherlockDialogFragment { 

    public static CategoriesDialogFragment newInstance(int title) { 
     CategoriesDialogFragment frag = new CategoriesDialogFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("title", title); 
     frag.setArguments(args); 
     return frag; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     int title = getArguments().getInt("title"); 

     return new AlertDialog.Builder(getActivity()) 
       .setIcon(R.drawable.alert_dialog_icon) 
       .setTitle(title) 
       .setMultiChoiceItems(_categories, _selections, 
         new DialogSelectionClickHandler()) 
       .setPositiveButton(R.string.alert_dialog_ok, 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int whichButton) { 
           ((MainActivity) getActivity()) 
             .doPositiveClick(); 
          } 
         }).create(); 

     /* 
     * .setNegativeButton(R.string.alert_dialog_cancel, new 
     * DialogInterface.OnClickListener() { public void 
     * onClick(DialogInterface dialog, int whichButton) { 
     * ((MainActivity) getActivity()) .doNegativeClick(); } }) 
     */ 
    } 

    public class DialogSelectionClickHandler implements 
      DialogInterface.OnMultiChoiceClickListener { 
     public void onClick(DialogInterface dialog, int clicked, 
       boolean selected) { 
      // Log.i("ME", _options[clicked] + " selected: " + selected); 
     } 
    } 

} 

,但我想添加的所有選項像圖像。所以我想我將不得不建立一個自定義的對話框。我是否仍然可以擴展原生setMultiChoiceItems,以便減少對代碼的處理。

+0

'Builder'有一個'setCustomTitle(View)'方法。 – Luksprog

+0

@Luksprog它需要有一個複選框。如圖中所示。所以當我點擊時,我想檢查或取消選中對話內容中的所有內容。 –

+0

那麼爲包含'TextView'(實際標題)和'all'' CheckBox'的方法提供一個View。 – Luksprog

回答

9

你也許可以使用AlertDialog.Builder類的setCustomTitle()方法,並構建自己的標題具有的標題文字,也是所有CheckBox,這樣的事情:

new AlertDialog.Builder(getActivity()) 
       .setIcon(R.drawable.alert_dialog_icon) 
       .setTitle(title) 
       .setCustomTitle(getLayoutInflater().inflate(R.layout.custom_title, null)); 

其中R.layout.custom_title是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     style="?android:attr/textAppearanceLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Dialog title" 
     android:textColor="#ffffff" /> 

    <TextView 
     android:id="@+id/all" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="All" 
     android:textColor="#ffffff" /> 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

其他風格的調整應該使它看起來更好。 但看到整個對話框佈局,您可能需要使用自定義的Dialog類,因此setMultiChoice()方法將不可用(但最終很容易複製)。

+0

類型DialogFragment中的方法getLayoutInflater(Bundle)不適用於參數() –

+0

@HarshaMV'getActivity()。getLayoutInflater()' – Luksprog

+0

我做了上述,我實現了我的自定義標題,但我怎麼才能得到值對象? findViewById不起作用:/ –

2

我已經實現了你在圖像中顯示的東西。我正在使用的是Custom Dialogue,因爲您正在考慮使用它。我在xml文件中使用了Listview。

首先定義初始化下面的數組列表。

// Catagory Selection 
    public static ArrayList<String> acceptpositionwhoesNearMe = new ArrayList<String>(); 
    public static String AcceptCatagotyIDWhoesNearMe = ""; 

下面是代碼的ShowDialog()我已經使用。

public void showDialog() { 
     Log.i(TAG, "Inside Show Dialog"); 
     final Dialog warning = new Dialog(logout_dialogue.this); 
     warning.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     warning.setContentView(R.layout.YOUR_XML); 
     warning.setCancelable(false); 
     warning.getWindow().setGravity(Gravity.CENTER); 

     WindowManager mWinMgr; 
     mWinMgr = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); 
     int displayWidth = mWinMgr.getDefaultDisplay().getWidth(); 

     warning.getWindow().setLayout(displayWidth - 75, 
       LayoutParams.WRAP_CONTENT); 
     warning.setOnDismissListener(new OnDismissListener() { 
      public void onDismiss(DialogInterface dialog) { 
       Log.i(TAG, "Inside Dialog interface"); 
       // test = true; 
       warning.dismiss(); 
      } 
     }); 
     ListView listinterest = (ListView) warning 
       .findViewById(R.id.list_catagory); 
     /* 
     * ArrayList<String> count = new ArrayList<String>(); count.clear(); 
     * count.add("Hotels"); count.add("Restaurants"); count.add("Gardens"); 
     * count.add("Theater"); 
     */ 
     CatagorySummaryAdapter adapter; 
     adapter = new CatagorySummaryAdapter(YOUR_ACTIVITY.this, 
       YOUR_ARRAYLIST_OF_CATAGORY, true); 

     listinterest.setAdapter(adapter); 

     Button btnOk = (Button) warning.findViewById(R.id.btn_close); 
     btnOk.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       // TODO Auto-generated method stub 

       String Catagory = ""; 
       int count = HomeActivity.acceptpositionwhoesNearMe.size(); 

       if (count > 0) { 
        for (int i = 0; i < HomeActivity.acceptpositionwhoesNearMe 
          .size(); i++) { 

         int pos = Integer 
           .parseInt(HomeActivity.acceptpositionwhoesNearMe 
             .get(i)); 
         if (Catagory.equals("")) { 
          Catagory = GetUserDetailsJsonParser.CategoryName 
            .get(pos); 
          HomeActivity.AcceptCatagotyIDWhoesNearMe = GetUserDetailsJsonParser.CategoryID 
            .get(pos); 
         } else { 

          Catagory = Catagory 
            + "," 
            + GetUserDetailsJsonParser.CategoryName 
              .get(pos); 
          HomeActivity.AcceptCatagotyIDWhoesNearMe = HomeActivity.AcceptCatagotyIDWhoesNearMe 
            + "," 
            + GetUserDetailsJsonParser.CategoryID 
              .get(pos); 
         } 

        } 

        Log.i(TAG, "Accept Catagory IDs WhoseNear Me" 
          + HomeActivity.AcceptCatagotyIDWhoesNearMe); 

        GetUserDetailsJsonParser.InterestedIn = HomeActivity.AcceptCatagotyIDWhoesNearMe; 
        UpdateMap = true; 
        /* 
        * startActivity(new Intent(WhosNearMe.this, 
        * BuildInukshk_4.class)); 
        */ 
        new GetUsersInRadiusAsyncTask().execute(); 

        warning.dismiss(); 

       } else { 
        Toast.makeText(WhosNearMe.this, 
          "Please Select One or More Catagory", 3).show(); 
       } 
       // test = true; 

      } 
     }); 

     warning.show(); 
    } 

這裏是我logout_dialogue.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/edittext_back_final" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <com.inukshk.CustomTextViewBold 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="10dp" 
       android:layout_marginTop="10dp" 
       android:text="Interested in" 
       android:textColor="#3C3C3C" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 
     </LinearLayout> 

     <ListView 
      android:id="@+id/list_catagory" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_weight="1" 
      android:cacheColorHint="#00000000" 
      android:divider="@android:color/transparent" > 
     </ListView> 

     <Button 
      android:id="@+id/btn_close" 
      android:layout_width="99dp" 
      android:layout_height="40dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="10dp" 
      android:background="@drawable/btn_back_final" 
      android:text="CLOSE" 
      android:textColor="#ffffff" 
      android:textSize="16dp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

</LinearLayout> 

這裏是我的CatagorySummaryAdapter.java

package com.inukshk.adapter; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

import com.inukshk.HomeActivity; 
import com.inukshk.R; 
import com.inukshk.CreateInukshk.BuildInukshk_3; 
import com.inukshk.WhosNearMe.WhosNearMe; 

public class CatagorySummaryAdapter extends BaseAdapter { 

    public Activity context; 
    String TAG = "CatagorySummaryAdapter"; 
    public LayoutInflater inflater; 
    public ArrayList<String> Count; 
    boolean Dialogue; 

    public CatagorySummaryAdapter(Activity context, ArrayList<String> Count, 
      boolean Dialogue) { 
     super(); 
     this.context = context; 
     this.inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     this.Count = Count; 

     this.Dialogue = Dialogue; 

     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return Count.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return Count.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public int getItemViewType(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public class ViewHolder { 
     RelativeLayout lsummary_row; 
     TextView txtinterestname; 
     CheckBox chkinterest; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     int pos = position; 
     ViewHolder holder; 

     if (convertView == null) { 
      holder = new ViewHolder(); 

      if (Dialogue) { 
       convertView = inflater.inflate(
         R.layout.listview_summary_dialogue_row, null); 
      } else { 
       convertView = inflater.inflate(R.layout.listview_summary_row, 
         null); 
      } 

      holder.txtinterestname = (TextView) convertView 
        .findViewById(R.id.txtinterestname); 
      holder.lsummary_row = (RelativeLayout) convertView 
        .findViewById(R.id.lsummary_row); 
      holder.chkinterest = (CheckBox) convertView 
        .findViewById(R.id.chkinterest); 
      holder.chkinterest.setEnabled(true); 

      holder.chkinterest.setTag(position); 

      if (Dialogue) { 

       for (int i = 0; i < HomeActivity.acceptpositionwhoesNearMe.size(); i++) { 
        int index = Integer 
          .parseInt(HomeActivity.acceptpositionwhoesNearMe 
            .get(i)); 
        // Log.i(TAG, "Inside for Loop of Accept Positions"); 
        if (index == position) { 
         // Log.i(TAG, "Matched for index" + index); 
         holder.chkinterest.setChecked(true); 
         holder.chkinterest 
           .setButtonDrawable(R.drawable.checkbox_checked); 
        } 

       } 

      } else { 
       for (int i = 0; i < BuildInukshk_3.acceptposition.size(); i++) { 
        int index = Integer.parseInt(BuildInukshk_3.acceptposition 
          .get(i)); 
        // Log.i(TAG, "Inside for Loop of Accept Positions"); 
        if (index == position) { 
         // Log.i(TAG, "Matched for index" + index); 
         holder.chkinterest.setChecked(true); 
         holder.chkinterest 
           .setButtonDrawable(R.drawable.checkbox_checked); 
        } 

       } 
      } 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.chkinterest 
       .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(CompoundButton buttonView, 
          boolean isChecked) { 
         // TODO Auto-generated method stub 
         // Log.i(TAG, "ISChecked is " + isChecked); 
         if (isChecked) { 
          // Log.i(TAG, "ISChecked is true"); 
          buttonView 
            .setButtonDrawable(R.drawable.checkbox_checked); 
          int position = Integer.parseInt(buttonView.getTag() 
            .toString()); 
          if (Dialogue) { 
           HomeActivity.acceptpositionwhoesNearMe.add(String 
             .valueOf(position)); 

           Log.i(TAG, "Accept ID of Dialogue" 
             + HomeActivity.acceptpositionwhoesNearMe); 
          } else { 
           BuildInukshk_3.acceptposition.add(String 
             .valueOf(position)); 

           Log.i(TAG, "Accept ID" 
             + BuildInukshk_3.acceptposition); 
          } 

         } else { 
          // Log.i(TAG, "ISChecked is false"); 
          buttonView 
            .setButtonDrawable(R.drawable.checkbox_unchecked); 
          int position = Integer.parseInt(buttonView.getTag() 
            .toString()); 

          if (Dialogue) { 
           if (HomeActivity.acceptpositionwhoesNearMe 
             .contains(String.valueOf(position))) { 
            // Log.i(TAG, 
            // "Inside Already present position"); 
            HomeActivity.acceptpositionwhoesNearMe 
              .remove(String.valueOf(position)); 
            Log.i(TAG, 
              "Accept ID Dialogue***" 
                + HomeActivity.acceptpositionwhoesNearMe); 
           } 
          } else { 
           if (BuildInukshk_3.acceptposition 
             .contains(String.valueOf(position))) { 
            // Log.i(TAG, 
            // "Inside Already present position"); 
            BuildInukshk_3.acceptposition.remove(String 
              .valueOf(position)); 
            Log.i(TAG, "Accept ID ***" 
              + BuildInukshk_3.acceptposition); 
           } 
          } 
          // 
         } 

        } 
       }); 

     holder.txtinterestname.setText(Count.get(pos)); 

     return convertView; 
    } 

} 

嘗試去通過它可以幫助你。

+0

是不是有可能在DialogFragments中使用setMultiChoiceItems? –

+0

@HarshaMV:我還沒有使用SherlockDialogFragment。所以不能給你正確的建議。真對不起。 –

+0

沒問題。你可以分享代碼做到全選,也可以不選全部。只要你提到你已經實現了這一點。我想我們可以很容易地將你的代碼移到DialogFragment –

0

我也想用setMultiChoiceItems()與我的對話,以保持,所以不是創建一個自定義對話框,我這樣做:

不要立即返回AlertDialog。創建AlertDialog.Builder它首先是這樣的:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

設置你的建設者參數:

builder.setTitle(listTitle) 
.setMultiChoiceItems(allTagsArray, containsTag, 
      new DialogInterface.OnMultiChoiceClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which, boolean isChecked){ 

然後創建AlertDialog本身調用create()。這不會顯示對話框。

AlertDialog dialog = builder.create();

然後拿到ListView,添加頁眉和/或頁腳,並將其返回。

ListView dialogList = dialog.getListView(); 
dialogList.addFooterView(yourHeader); 
return dialog; 

這也適用於@ Luksprog的setCustomTitle()給你一共有三個定製空間,而無需實現自定義適配器。對於某些人來說,唯一可能的缺點是頁眉和頁腳與列表一起滾動(顯然,如果列表比屏幕更長,這隻會產生問題)。

希望這會有所幫助。

重要更新: 請勿在複選框列表中使用標題。它導致AlertController.java錯誤地計算了哪個複選框被打勾,使其無效。一個頁腳仍然可以使用,在這種情況下非常有用,但在我的情況下不是這樣,所以我正在構建一個自定義對話框。