Android的 - 搜索查看里面的操作栏自动完成功能 [英] Android - searchview with auto complete feature inside action bar

查看:138
本文介绍了Android的 - 搜索查看里面的操作栏自动完成功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个应用程序,具有福尔摩斯操作栏和里面搜索视图。不过,我想这个搜索查看有像什么autocompleteTextView具有自动完成功能。是否有任何直接的实现做单独使用搜索查看?或者我应该使用autocompleteTextView内部操作栏做到这一点?我发现了几个职位在那里,但他们都不是在帮助我。这些职位只讲创建autocompleteTextView,但我想搜索,以具有此功能。任何想法如何能不能做到?

I want to build an app that has sherlock action bar and a search view inside it. However, i want this searchview to have autocomplete feature like what autocompleteTextView has. Is there any straight forward implementation to do that using searchview alone? or should i use autocompleteTextView inside action bar to do this? I found several posts out there, but none of them are helping me. Those posts talk only about creating a autocompleteTextView , but i want search view to have this functionality. Any idea how can it be done?

推荐答案

对于这个我有一个 AutoCompleteTextView 创建一个布局和动作条添加呼吁在动作条自定义布局。

For this I have Create one Layout with AutoCompleteTextView and add it in ActionBar its call Custom layout in ActionBar.

在我有创建适配器与 android.R.layout.simple_dropdown_item_1line 。设置在 AutoCompleteTextView

After that I have Create Adapter with android.R.layout.simple_dropdown_item_1line. set it in AutoCompleteTextView.

检查以下code:

package com.example.testapp;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

@TargetApi(11)
public class MainActivity extends Activity {

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

    @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.actionbar, 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);

    }

}

您的布局:

<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>

有关更多详细信息检查该小说章节导航 之一,的two 和<一href="http://stackoverflow.com/questions/12883732/how-to-display-custom-view-in-actionbar">three

For More Details check this articals: one, two and three

祝您好运!

这篇关于Android的 - 搜索查看里面的操作栏自动完成功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆