2014-01-15 62 views
1

我可以在Android中使用多個值填充多個1 spinner。我想要在微調器中顯示多於1個值,即在「0」位置(微調索引)我想根據用戶動態設置多個值。當用戶選擇將設置爲0位置的第一個值並且當用戶單擊第二個值時,它將被添加到相同的0位置而不替換它。這樣兩個值可以被看到並且可以被使用。將多個值設置爲Android中的微調器

它可能在android微調嗎?如果有任何建議,請..

+0

是的,可能的。你正在尋找的術語是「有多個選擇的微調」。請參閱http://stackoverflow.com/questions/5015686/android-spinner-with-multiple-choice和http://v4all123.blogspot.in/2013/09/spinner-with-multiple-selection-in的教程。 html –

+0

謝謝,我會檢查並讓你知道:) –

+0

你還在掙扎嗎? –

回答

2

您可以按照

http://labs.makemachine.net/2010/03/android-multi-selection-dialogs/http://v4all123.blogspot.in/2013/09/spinner-with-multiple-selection-in.html。讓我知道如果有任何錯誤? 檢查這一點,我認爲這是你在尋找什麼 http://codethis.wordpress.com/2012/06/17/a-spinner-control-for-android-with-multi-select-support/

在MultiSelectionSpinner類

import java.util.Arrays; 
import java.util.LinkedList; 
import java.util.List; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnMultiChoiceClickListener; 
import android.util.AttributeSet; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.SpinnerAdapter; 

public class MultiSelectionSpinner extends Spinner implements OnMultiChoiceClickListener { 
    String[] _items = null; 

    boolean[] mSelection = null; 

    ArrayAdapter<String> simple_adapter; 

    public MultiSelectionSpinner(Context context) { 
     super(context); 

     simple_adapter = new ArrayAdapter<String>(context, 
       android.R.layout.simple_spinner_item); 
     super.setAdapter(simple_adapter); 
    } 

    public MultiSelectionSpinner(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     simple_adapter = new ArrayAdapter<String>(context, 
       android.R.layout.simple_spinner_item); 
     super.setAdapter(simple_adapter); 
    } 

    public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
     if (mSelection != null && which < mSelection.length) { 
      mSelection[which] = isChecked; 

      simple_adapter.clear(); 
      simple_adapter.add(buildSelectedItemString()); 
     } else { 
      throw new IllegalArgumentException( 
      "Argument 'which' is out of bounds."); 
     } 
    } 

    @Override 
    public boolean performClick() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 
     builder.setMultiChoiceItems(_items, mSelection, this); 
     builder.show(); 
     return true; 
    } 

    @Override 
    public void setAdapter(SpinnerAdapter adapter) { 
     throw new RuntimeException( 
     "setAdapter is not supported by MultiSelectSpinner."); 
    } 

    public void setItems(String[] items) { 
     _items = items; 
     mSelection = new boolean[_items.length]; 
     simple_adapter.clear(); 
     simple_adapter.add(_items[0]); 
     Arrays.fill(mSelection, false); 
    } 

    public void setItems(List<String> items) { 
     _items = items.toArray(new String[items.size()]); 
     mSelection = new boolean[_items.length]; 
     simple_adapter.clear(); 
     simple_adapter.add(_items[0]); 
     Arrays.fill(mSelection, false); 
    } 

    public void setSelection(String[] selection) { 
     for (String cell : selection) { 
      for (int j = 0; j < _items.length; ++j) { 
       if (_items[j].equals(cell)) { 
        mSelection[j] = true; 
       } 
      } 
     } 
    } 

    public void setSelection(List<String> selection) { 
     for (int i = 0; i < mSelection.length; i++) { 
      mSelection[i] = false; 
     } 
     for (String sel : selection) { 
      for (int j = 0; j < _items.length; ++j) { 
       if (_items[j].equals(sel)) { 
        mSelection[j] = true; 
       } 
      } 
     } 
     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public void setSelection(int index) { 
     for (int i = 0; i < mSelection.length; i++) { 
      mSelection[i] = false; 
     } 
     if (index >= 0 && index < mSelection.length) { 
      mSelection[index] = true; 
     } else { 
      throw new IllegalArgumentException("Index " + index 
        + " is out of bounds."); 
     } 
     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public void setSelection(int[] selectedIndicies) { 
     for (int i = 0; i < mSelection.length; i++) { 
      mSelection[i] = false; 
     } 
     for (int index : selectedIndicies) { 
      if (index >= 0 && index < mSelection.length) { 
       mSelection[index] = true; 
      } else { 
       throw new IllegalArgumentException("Index " + index 
         + " is out of bounds."); 
      }  
     } 
     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public List<String> getSelectedStrings() { 
     List<String> selection = new LinkedList<String>(); 
     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       selection.add(_items[i]); 
      } 
     } 
     return selection; 
    } 

    public List<Integer> getSelectedIndicies() { 
     List<Integer> selection = new LinkedList<Integer>(); 
     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       selection.add(i); 
      } 
     } 
     return selection; 
    } 

    private String buildSelectedItemString() { 
     StringBuilder sb = new StringBuilder(); 
     boolean foundOne = false; 

     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       if (foundOne) { 
        sb.append(", "); 
       } 
       foundOne = true; 

       sb.append(_items[i]); 
      } 
     } 
     return sb.toString(); 
    } 

    public String getSelectedItemsAsString() { 
     StringBuilder sb = new StringBuilder(); 
     boolean foundOne = false; 

     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       if (foundOne) { 
        sb.append(", "); 
       } 
       foundOne = true; 
       sb.append(_items[i]); 
      } 
     } 
     return sb.toString(); 
    } 
} 
+0

你能解釋一些代碼嗎? –

+0

這實際上不是問題的答案 –

+0

@ ling.s我是android的新手,並且引起了很多混淆。因此,您能否給出一些示例代碼,以便如何通過將值分別與「,」或少數說明??? –

1

吹方案如下: -

package com.kazi.createActivity; 


import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnMultiChoiceClickListener; 
import android.util.AttributeSet; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 

import com.kazi.R; 

import java.util.List; 


public class MultiSpinner extends Spinner implements 
     OnMultiChoiceClickListener, DialogInterface.OnCancelListener { 

    private List<String> items; 
    private boolean[] selected; 
    private String defaultText; 
    private MultiSpinnerListener listener; 

    public MultiSpinner(Context context) { 
     super(context); 
    } 

    public MultiSpinner(Context arg0, AttributeSet arg1) { 
     super(arg0, arg1); 
    } 

    public MultiSpinner(Context arg0, AttributeSet arg1, int arg2) { 
     super(arg0, arg1, arg2); 
    } 

    @Override 
    public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
     if (isChecked) 
      selected[which] = true; 
     else 
      selected[which] = false; 
    } 

    @Override 
    public void onCancel(DialogInterface dialog) { 
     // refresh text on spinner 
     StringBuffer spinnerBuffer = new StringBuffer(); 
     boolean someUnselected = false; 
     for (int i = 0; i < items.size(); i++) { 
      if (selected[i] == true) { 
       spinnerBuffer.append(items.get(i)); 
       spinnerBuffer.append(", "); 
      } else { 
       someUnselected = true; 
      } 
     } 
     String spinnerText; 
     if (someUnselected) { 
      spinnerText = spinnerBuffer.toString(); 
      if (spinnerText.length() > 2) 
       spinnerText = spinnerText.substring(0, spinnerText.length() - 2); 
     } else { 
      spinnerText = defaultText; 
     } 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), 
       R.layout.spinner_list_item, 
       new String[] { spinnerText }); 
     setAdapter(adapter); 
     listener.onItemsSelected(selected); 
    } 

    @Override 
    public boolean performClick() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 
     builder.setMultiChoiceItems(
       items.toArray(new CharSequence[items.size()]), selected, this); 
     builder.setPositiveButton(android.R.string.ok, 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 
     builder.setOnCancelListener(this); 
     builder.show(); 
     return true; 
    } 

    public void setItems(List<String> items, String[] allText, 
         MultiSpinnerListener listener) { 
     this.items = items; 

     this.listener = listener; 

     // all selected by default 
     selected = new boolean[items.size()]; 
     for (int i = 0; i < selected.length; i++) 
      selected[i] = false; 

     // all text on the spinner 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), 
       R.layout.spinner_list_item, allText); 
     setAdapter(adapter); 
    } 

    public interface MultiSpinnerListener { 
     public void onItemsSelected(boolean[] selected); 
    } 
} 

我創造了這個XML佈局: -

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/spiner_item" 
android:layout_width="match_parent" 
android:layout_height="@dimen/spiner_height" 
android:background="@drawable/rounded_edittext" 
android:gravity="center_vertical" 
android:paddingLeft="5dp" 
android:text="Medium Text" 
android:textAppearance="?android:attr/textAppearanceMedium" 
android:textColor="@android:color/black" /> 
相關問題