如何在Android中顯示組合框?如何在Android中顯示組合框?
回答
我建議你考慮這個在android環境下發展。 http://www.designerandroid.com/?p=8。在android dev的上下文中,它被稱爲微調器。請下次做你的研究。 – gruntled 2011-05-19 16:56:02
我不認爲這個來源是授權的。相反,一些更多的權威人士告訴我,否則:http://developer.android.com/guide/topics/ui/custom-components.html – vbence 2011-05-19 19:52:49
是的,通過查看你自己提供的網站,你可以看到他們做提到該頁面上的ComboBox,但在API中只有對Spinner的引用(http://developer.android.com/resources/tutorials/views/hello-spinner.html)在這裏他們明確指出「Spinner是一個類似於用於選擇項目的下拉列表的小部件「。我同意你和其他Java實現一樣被稱爲ComboBox,但在這種情況下它不是。 – gruntled 2011-05-19 20:12:20
未經測試,但您可以近距離看到的是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")
,所以你不需要管理緩衝區列表。
這段代碼並沒有接近編譯。對getFilter()的調用看起來像是無限循環的嘗試,publishResults從void方法返回一個值。這個想法總體上不錯,但有人應該修正這個例子。 – dhakim 2014-03-21 19:59:18
這是自定義組合框的例子在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);
}
}
希望它可以幫助!
謝謝你的回覆。我想使用這個小部件,但我想使用一個字符串數組作爲數據源而不是遊標。我該怎麼辦? – 2013-09-14 15:13:13
對於允許自由文本輸入並具有下拉列表框的組合框(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);
}
的問題是完全有效和明確的,因爲微調和組合框(閱讀:微調在那裏你可以提供一個自定義的值也一樣)是兩個不同的東西。
我一直在尋找同樣的東西,我對給定的答案並不滿意。所以我創造了我自己的東西。也許有些人會發現以下提示很有用。我沒有提供完整的源代碼,因爲我在自己的項目中使用了一些傳統調用。無論如何,它應該很清楚。
這裏的最後一件事截圖:
的第一件事是創建一個視圖,將看起來一樣尚未展開微調。在截圖中,在屏幕的頂部(焦點不清),您可以看到微調器和自定義視圖在下面。爲此,我使用了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
);
更多或更少,這應該是吧。
看起來不錯。我正在嘗試實現你的解決方案,但我對android開發很陌生,對於把代碼片段放在哪裏我有點困惑。你介意修改一下來解釋如何實現它嗎? – 2016-06-01 17:28:35
定製:) 您可以使用下拉式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>
那有用嗎?
- 1. 如何顯示組合框中的值
- 2. 顯示在vb.net組合框
- 3. 如何在文本框或組合框中顯示多個值
- 4. 如何在Silverlight組合框中顯示圖像數組?
- 5. 如何在Silverlight組合框中顯示項目組標題?
- 6. 如何顯示組合框selectedvalue?
- 7. 如何顯示組合框消息
- 8. 如何更改組合框顯示
- 9. 如何在WPF中的組合框中顯示下拉列表?
- 10. 如何在Silverlight中顯示組合框中的值?
- 11. 如何在php中顯示組合框中的選定值
- 12. 如何在綁定時在ListView中顯示組合框?
- 13. 如何在組合框中僅顯示不在表C#
- 14. 如何在組合框中輸入文本時在組合框中顯示匹配的值集合?
- 15. 如何顯示文本框C#中的組合框選定值?
- 16. 組合框顯示system.data.datarowview vb.NET
- 17. VB.NET組合框顯示System.Data.DataRowView
- 18. 組合框顯示部件
- 19. 組合框突出顯示
- 20. 顯示AudioCaptureDevices的組合框
- 21. 組合框不顯示DisplayMember
- 22. 組合框SelectedItem顯示
- 23. VB.NET如何顯示在組合框中正確的數據
- 24. 如何在WinForms(Telerik)中顯示組合框的DropDown列表
- 25. 如何使用綁定在組合框中顯示默認值?
- 26. 如何在ExtJS組合框中顯示水平滾動條?
- 27. 如何在組合框中顯示地理細節?
- 28. 如何在組合框中顯示空白項目?
- 29. WPF - 如何在突出顯示組合框時集中
- 30. 如何在標籤c#中顯示組合框的選定值?
請更清楚地解釋你想要什麼。而你已經嘗試過。 – fretje 2010-06-11 16:58:55
@fretje這個問題非常具體。如果你知道** ComboBox **是什麼,你不需要解釋。如果你還沒有,你還可以谷歌它:http://en.wikipedia.org/wiki/Combo_box – vbence 2011-05-19 10:35:44
@vbence:我不是在談論組合框。由於Android是一個操作系統,你可以問「如何在Windows中顯示組合框」,這根本不是特定的。 – fretje 2011-05-19 11:27:47