如何在Android中显示组合框?

Jam*_*mes 74 android combobox

如何在Android中显示组合框?

gru*_*led 72

在android中,它被称为Spinner,您可以在这里查看教程.

你好,旋转器

这是一个非常模糊的问题,您应该尝试更好地描述您的问题.

  • 我建议你在android开发的上下文中考虑这个.http://www.designerandroid.com/?p=8.在android dev的上下文中,它被称为Spinner.请你下次做研究. (17认同)
  • 停止挑剔并承认每个人在第一次编写Android应用程序时找到一个类似于组合框或列表框的控件时遇到问题...... (14认同)
  • 我还在寻找一个组合框..我见过Spinners.二手纺纱机.但坦率地说,我有一个场景,我需要将文本设置为除给定选项之外的其他内容.阿卡,他们可以输入它.旋转器不是组合盒,但它们通常是有效的替代品. (10认同)
  • 是的,通过查看您自己提供的网站,您可以看到他们确实提到了该页面上的ComboBox,但在API中只有Spinner的引用(http://developer.android.com/resources/tutorials /views/hello-spinner.html)这里他们明确指出"Spinner是一个类似于下拉列表的小部件,用于选择项目".我同意你的观点,这应该像其他Java实现一样被称为ComboBox,但在这种情况下它不是. (3认同)
  • 据我所知,隐喻在移动UI方面有所改变.有新的小部件(控件)可以更好地利用有限的屏幕空间.我猜这就是为什么用于桌面隐喻所熟悉的某些控件的新名称. - 我同意Spinner类似于下拉列表.但是,*ListBox*(下拉列表)和*ComboBox*之间的主要区别在于组合框基本上是一个扩展的文本输入字段,可以从列表中进行选择.*您可以从列表中选择一个元素,也可以输入任意值.* (3认同)

Ran*_*med 11

以下是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);
   }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!!


vbe*_*nce 7

没有经过测试,但是你可以获得的距离越近似乎是使用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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

...然后在你的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);
Run Code Online (Sandbox Code Playgroud)

代码没有经过测试,可能有一些我没有考虑的过滤方法的功能,但是你有它,模拟带有AutoCompleteTextView的ComboBox的基本原则.

编辑 Fixed NullFilter实现.我们需要访问项目,因此构造函数UnconditionalArrayAdapter需要引用List(缓冲区的种类).您也可以使用eg adapter = new UnconditionalArrayAdapter<String>(..., new ArrayList<String>);然后使用adapter.add("Luxemburg"),因此您不需要管理缓冲区列表.


Vik*_*šan 6

Spinner和ComboBox(读它:Spinner,你可以提供自定义值)是完全有效和清晰的两个不同的东西.

我自己也在寻找相同的东西,我对给定的答案并不满意.所以我创造了自己的东西.也许有些人会发现以下提示有用.我没有提供完整的源代码,因为我在自己的项目中使用了一些遗留调用.反正应该很清楚.

这是最终的截图:

Android上的ComboBox

第一件事是创建一个看起来与尚未展开的微调器相同的视图.在屏幕截图中,在屏幕顶部(失焦),您可以看到微调器和自定义视图.为此,我使用了LinearLayout(实际上,我继承了Linear Layout)style="?android:attr/spinnerStyle".LinearLayout包含TextView style="?android:attr/spinnerItemStyle".完整的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>
Run Code Online (Sandbox Code Playgroud)

正如我之前提到的,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>
Run Code Online (Sandbox Code Playgroud)

您需要实现另外两个侦听器:onItemClick用于列表,onClick用于按钮.这两个都设置了所选值并关闭了对话框.

对于列表,您希望它看起来与扩展的Spinner相同,您可以这样做,为列表适配器提供适当的(Spinner)样式,如下所示:

ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(
        activity,
        android.R.layout.simple_spinner_dropdown_item, 
        states
    );
Run Code Online (Sandbox Code Playgroud)

或多或少,应该是它.