2010-06-11 96 views
53

如何在Android中显示组合框?如何在Android中显示组合框?

+1

请更清楚地解释你想要什么。而你已经尝试过。 – fretje 2010-06-11 16:58:55

+24

@fretje这个问题非常具体。如果你知道** ComboBox **是什么,你不需要解释。如果你还没有,你还可以谷歌它:http://en.wikipedia.org/wiki/Combo_box – vbence 2011-05-19 10:35:44

+1

@vbence:我不是在谈论组合框。由于Android是一个操作系统,你可以问“如何在Windows中显示组合框”,这根本不是特定的。 – fretje 2011-05-19 11:27:47

回答

56

在android中,它被称为微调,你可以看看这里的教程。

Hello, Spinner

这是一个非常模糊的问题,你应该尝试更多的描述你的问题。

+15

我建议你考虑这个在android环境下发展。 http://www.designerandroid.com/?p=8。在android dev的上下文中,它被称为微调器。请下次做你的研究。 – gruntled 2011-05-19 16:56:02

+0

我不认为这个来源是授权的。相反,一些更多的权威人士告诉我,否则:http://developer.android.com/guide/topics/ui/custom-components.html – vbence 2011-05-19 19:52:49

+3

是的,通过查看你自己提供的网站,你可以看到他们做提到该页面上的ComboBox,但在API中只有对Spinner的引用(http://developer.android.com/resources/tutorials/views/hello-spinner.html)在这里他们明确指出“Spinner是一个类似于用于选择项目的下拉列表的小部件“。我同意你和其他Java实现一样被称为ComboBox,但在这种情况下它不是。 – gruntled 2011-05-19 20:12:20

6

未经测试,但您可以近距离看到的是AutoCompleteTextView。您可以编写一个适配器,以忽略过滤器功能。喜欢的东西:

class UnconditionalArrayAdapter<T> extends ArrayAdapter<T> { 
    final List<T> items; 
    public UnconditionalArrayAdapter(Context context, int textViewResourceId, List<T> items) { 
     super(context, textViewResourceId, items); 
     this.items = items; 
    } 

    public Filter getFilter() { 
     return new NullFilter(); 
    } 

    class NullFilter extends Filter { 
     protected Filter.FilterResults performFiltering(CharSequence constraint) { 
      final FilterResults results = new FilterResults(); 
      results.values = items; 
      return results; 
     } 

     protected void publishResults(CharSequence constraint, Filter.FilterResults results) { 
      items.clear(); // `items` must be final, thus we need to copy the elements by hand. 
      for (Object item : (List) results.values) { 
       items.add((String) item); 
      } 
      if (results.count > 0) { 
       notifyDataSetChanged(); 
      } else { 
       notifyDataSetInvalidated(); 
      } 
     } 
    } 
} 

...然后在您的onCreate:

String[] COUNTRIES = new String[] {"Belgium", "France", "Italy", "Germany"}; 
List<String> contriesList = Arrays.asList(COUNTRIES()); 
ArrayAdapter<String> adapter = new UnconditionalArrayAdapter<String>(this, 
    android.R.layout.simple_dropdown_item_1line, contriesList); 
AutoCompleteTextView textView = (AutoCompleteTextView) 
    findViewById(R.id.countries_list); 
textView.setAdapter(adapter); 

的代码没有经过测试,可以有一些特征与滤波方法我没有考虑,但你有它,用AutoCompleteTextView模拟ComboBox的基本原理。

编辑 修复了NullFilter实现。 我们需要对项目进行访问,因此UnconditionalArrayAdapter的构造函数需要引用List(缓冲区的类型)。 您也可以使用例如adapter = new UnconditionalArrayAdapter<String>(..., new ArrayList<String>);,然后使用adapter.add("Luxemburg"),所以你不需要管理缓冲区列表。

+0

这段代码并没有接近编译。对getFilter()的调用看起来像是无限循环的尝试,publishResults从void方法返回一个值。这个想法总体上不错,但有人应该修正这个例子。 – dhakim 2014-03-21 19:59:18

10

这是自定义组合框的例子在android系统:

package myWidgets; 
import android.content.Context; 
import android.database.Cursor; 
import android.text.InputType; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.AutoCompleteTextView; 
import android.widget.ImageButton; 
import android.widget.LinearLayout; 
import android.widget.SimpleCursorAdapter; 

public class ComboBox extends LinearLayout { 

    private AutoCompleteTextView _text; 
    private ImageButton _button; 

    public ComboBox(Context context) { 
     super(context); 
     this.createChildControls(context); 
    } 

    public ComboBox(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.createChildControls(context); 
} 

private void createChildControls(Context context) { 
    this.setOrientation(HORIZONTAL); 
    this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 

    _text = new AutoCompleteTextView(context); 
    _text.setSingleLine(); 
    _text.setInputType(InputType.TYPE_CLASS_TEXT 
        | InputType.TYPE_TEXT_VARIATION_NORMAL 
        | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES 
        | InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE 
        | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT); 
    _text.setRawInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); 
    this.addView(_text, new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT, 1)); 

    _button = new ImageButton(context); 
    _button.setImageResource(android.R.drawable.arrow_down_float); 
    _button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        _text.showDropDown(); 
      } 
    }); 
    this.addView(_button, new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 
} 

/** 
    * Sets the source for DDLB suggestions. 
    * Cursor MUST be managed by supplier!! 
    * @param source Source of suggestions. 
    * @param column Which column from source to show. 
    */ 
public void setSuggestionSource(Cursor source, String column) { 
    String[] from = new String[] { column }; 
    int[] to = new int[] { android.R.id.text1 }; 
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this.getContext(), 
        android.R.layout.simple_dropdown_item_1line, source, from, to); 
    // this is to ensure that when suggestion is selected 
    // it provides the value to the textbox 
    cursorAdapter.setStringConversionColumn(source.getColumnIndex(column)); 
    _text.setAdapter(cursorAdapter); 
} 

/** 
    * Gets the text in the combo box. 
    * 
    * @return Text. 
    */ 
public String getText() { 
    return _text.getText().toString(); 
} 

/** 
    * Sets the text in combo box. 
    */ 
public void setText(String text) { 
    _text.setText(text); 
    } 
} 

希望它可以帮助!

+1

谢谢你的回复。我想使用这个小部件,但我想使用一个字符串数组作为数据源而不是游标。我该怎么办? – 2013-09-14 15:13:13

0

对于允许自由文本输入并具有下拉列表框的组合框(http://en.wikipedia.org/wiki/Combo_box),我使用了vbence建议的AutoCompleteTextView

我用onClickListener在用户选择控件时显示下拉列表框。

我相信这最接近这种组合框。

private static final String[] STUFF = new String[] { "Thing 1", "Thing 2" }; 

public void onCreate(Bundle b) { 
    final AutoCompleteTextView view = 
     (AutoCompleteTextView) findViewById(R.id.myAutoCompleteTextView); 

    view.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
       view.showDropDown(); 
     } 
    }); 

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
     this, 
     android.R.layout.simple_dropdown_item_1line, 
     STUFF 
    ); 
    view.setAdapter(adapter); 
} 
5

的问题是完全有效和明确的,因为微调和组合框(阅读:微调在那里你可以提供一个自定义的值也一样)是两个不同的东西。

我一直在寻找同样的东西,我对给定的答案并不满意。所以我创造了我自己的东西。也许有些人会发现以下提示很有用。我没有提供完整的源代码,因为我在自己的项目中使用了一些传统调用。无论如何,它应该很清楚。

这里的最后一件事截图:

ComboBox on Android

的第一件事是创建一个视图,将看起来一样尚未展开微调。在截图中,在屏幕的顶部(焦点不清),您可以看到微调器和自定义视图在下面。为此,我使用了LinearLayout(实际上,我从Linear Layout继承)与style="?android:attr/spinnerStyle"。LinearLayout包含带有style="?android:attr/spinnerItemStyle"的TextView。完整的XML代码片段如下:

<com.example.comboboxtest.ComboBox 
    style="?android:attr/spinnerStyle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 

    <TextView 
     android:id="@+id/textView" 
     style="?android:attr/spinnerItemStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ellipsize="marquee" 
     android:singleLine="true" 
     android:text="January" 
     android:textAlignment="inherit" 
    /> 

</com.example.comboboxtest.ComboBox> 

正如我前面提到的,ComboBox继承了LinearLayout。它还实现了OnClickListener,它创建一个对话框,其中包含从XML文件膨胀的自定义视图。这里是膨胀视图:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     > 
     <EditText 
      android:id="@+id/editText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:hint="Enter custom value ..." > 

      <requestFocus /> 
     </EditText> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="OK" 
     /> 
    </LinearLayout> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
    /> 

</LinearLayout> 

还有两个需要实现的侦听器:onItemClick为列表和onClick为按钮。这两个都设置选定的值并关闭对话框。

对于列表,你希望它看起来一样展开微调,你可以做到这一点,提供列表适配器与适当的(微调)的风格是这样的:

ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(
     activity, 
     android.R.layout.simple_spinner_dropdown_item, 
     states 
    ); 

更多或更少,这应该是吧。

+0

看起来不错。我正在尝试实现你的解决方案,但我对android开发很陌生,对于把代码片段放在哪里我有点困惑。你介意修改一下来解释如何实现它吗? – 2016-06-01 17:28:35

2

定制:) 您可以使用下拉式hori /垂直偏移属性来定位当前列表, 也可以尝试android:spinnerMode =“dialog”它更酷。

布局

<LinearLayout 
     android:layout_marginBottom="20dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <AutoCompleteTextView 
      android:layout_weight="1" 
      android:id="@+id/edit_ip" 
      android:text="default value" 
      android:layout_width="0dp" 
      android:layout_height= "wrap_content"/> 
     <Spinner 
      android:layout_marginRight="20dp" 
      android:layout_width="30dp" 
      android:layout_height="50dp" 
      android:id="@+id/spinner_ip" 
      android:spinnerMode="dropdown" 
      android:entries="@array/myarray"/> 
</LinearLayout> 

的Java

  //set auto complete 
     final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit_ip); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, getResources().getStringArray(R.array.myarray)); 
     textView.setAdapter(adapter); 
     //set spinner 
     final Spinner spinner = (Spinner) findViewById(R.id.spinner_ip); 
     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       textView.setText(spinner.getSelectedItem().toString()); 
       textView.dismissDropDown(); 
      } 
      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 
       textView.setText(spinner.getSelectedItem().toString()); 
       textView.dismissDropDown(); 
      } 
     }); 

RES /价值/串

<string-array name="myarray"> 
    <item>value1</item> 
    <item>value2</item> 
</string-array> 

那有用吗?

相关问题