8

在我的应用程序中,我使用的是自动完成文本。当用户键入t时,它会显示五十,但我想显示而不是五十。谁能帮我解决这个问题。

String search[] = {"one","two","three","four","five","six","seven","fifty"};    

ArrayAdapter<String> adp= new ArrayAdapter<String>(this,R.layout.searchtext, search);
    search_item.setThreshold(1);
    search_item.setAdapter(adp);
4

2 回答 2

6

我认为您不必创建自定义适配器。您想要的默认功能。检查这个

在这里我有像这样的数组:

private static final String[] COUNTRIES = new String[] { "Belgium",
        "France", "France_", "Italy", "Germany", "Spain","abcf","FFa","FFb","bFF","aFF" };

当我搜索 ff 然后它只给我2("FFa","FFb")建议不是4("FFa","FFb","bFF","aFF")

在此处输入图像描述

我在 ActionBar 中使用过这个。您也可以在布局中使用它。因为它在 ActionBar 中的自定义布局。

public class TestActivity extends Activity {

    /** Called when the activity is first created. */
    private static final String[] COUNTRIES = new String[] { "Belgium",
        "France", "France_", "Italy", "Germany", "Spain","abcf","FFa","FFb","bFF","aFF" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        // actionBar.setDisplayShowTitleEnabled(false);
        // actionBar.setIcon(R.drawable.ic_action_search);

        LayoutInflater inflator = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.test, null);

        actionBar.setCustomView(v);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);
        AutoCompleteTextView textView = (AutoCompleteTextView) v
                .findViewById(R.id.editText1);
        textView.setAdapter(adapter);

    }
}

测试.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Action Bar:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

    <AutoCompleteTextView
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:imeOptions="actionSearch"
        android:inputType="textAutoComplete|textAutoCorrect"
        android:textColor="#FFFFFF" >

        <requestFocus />
    </AutoCompleteTextView>

</LinearLayout>
于 2013-04-25T07:12:19.810 回答
0

在科特林:

我对缩写视图使用数据绑定

xml:

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInput_type"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:hint="@string/type">

            <AutoCompleteTextView
                android:id="@+id/act_type"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/light"
                android:ems="10"
                android:editable="false"
                android:text=""
                android:textSize="12sp" />
        </com.google.android.material.textfield.TextInputLayout>

片段:

class MyFragment : Fragment() {
    var orderType = arrayOf("فروش", "خرید")
    lateinit var mBinding: MyFragment

    override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    mBinding = MyFragment.inflate(inflater, container, false)
    val adapter= ArrayAdapter(requireContext(),android.R.layout.simple_spinner_item, orderType.toList())
    mBinding.autoOrderType.threshold = 0
    mBinding.autoOrderType.setAdapter(adapter)
    return mBinding.root
}
于 2020-11-15T08:57:55.207 回答