为搜索范围的搜索自定义Android搜索小部件

时间:2014-05-11 09:41:11

标签: android android-layout search

我想在我的Android应用程序中实现搜索过滤器。我已经了解了如何集成搜索过滤器并能够理解如何将其集成到应用程序中的示例。但我的要求是在进行搜索时提供基于过滤器的范围搜索。我试图搜索类似的实现,但无法找到任何示例。请查看此UI Pattern集合中的范围搜索部分,尤其是iPhone的Dropbox示例。

如前所述,我无法在Android中找到类似的例子但是通过查看Dictionary.com的应用程序(下面显示的快照),我开始知道它在android中的可能性(当然通过添加更多的努力以防万一)它不可能与搜索小部件本身)。任何人都可以提供任何指示我如何在我的应用程序中实现类似的范围搜索?

感谢你花时间在这上面。

enter image description here

2 个答案:

答案 0 :(得分:1)

我会做以下事情: 首先我为alertdialog创建一个searchType布局,选择:(图片,视频等) 然后我创建搜索活动并实现小部件(如在android指南上)。

在活动中创建一个变量:

private String searchType = "";

然后

...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.YOUR_MENU, menu);
    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat
            .getActionView(menuItem);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
          //HERE INSERT THE CODE ABOUT THE ALERT DIALOG FOR THE CHOOSE 
          //THEN INSERT THE aALERT DIALOG RESPONSE INTO THE searchType VARIABLE
        }
}

答案 1 :(得分:0)

我创建了一个不使用内置搜索功能的自定义搜索小部件。它易于实现,可以为当前活动提供信息。 它还使用自动填充文本视图,因此您可以使用自动填充功能,也可以使用普通的EditText替换它。

public class CustomViewSearch extends View {

private CustomAutoCompleteView searchEditText;
private boolean viewShown = false;
private ActionBar actionBar;
private InputMethodManager inputMethodManager;
private OnEditorActionSearchListener onEditorActionSearchListener;
private List<String> dataItems;
private ArrayAdapter<String> arrayAdapter;

public interface OnEditorActionSearchListener {
    void onEditorActionSearch(String searchText);

    void onTextChangedListener(String text);

    List<String> getNewItemsForSuggestions(String text);
}

public void setOnEditorActionSearchListener(OnEditorActionSearchListener l) {
    onEditorActionSearchListener = l;
}

// IMPORTANT: Provide your activity as the context
public CustomViewSearch(final Context context, List<String> items) {
    super(context);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View searchView = inflater.inflate(R.layout.custom_view_search_widget, null);

    actionBar = ((ActionBarActivity) context).getSupportActionBar();
    inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setCustomView(searchView);
    actionBar.setDisplayShowCustomEnabled(true);

    dataItems = items;
    searchEditText = (CustomAutoCompleteView) searchView.findViewById(R.id.edit_text_search);

    searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                onEditorActionSearchListener.onEditorActionSearch(searchEditText.getText().toString());
                return true;
            }
            return false;
        }
    });
    searchEditText.requestFocus();
    searchEditText.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            onEditorActionSearchListener.onTextChangedListener(s.toString());
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            dataItems = onEditorActionSearchListener.getNewItemsForSuggestions(s.toString());

            // update the adapater
            arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, dataItems);
            arrayAdapter.notifyDataSetChanged();

            searchEditText.setAdapter(arrayAdapter);
        }
    });
    ImageButton closeImageButton = (ImageButton) searchView.findViewById(R.id.image_button_search_close);
    closeImageButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (searchEditText.getText().length() > 0) {
                searchEditText.setText("");
            } else {
                hideKeyboard();
                actionBar.setDisplayShowTitleEnabled(true);
                actionBar.setDisplayShowCustomEnabled(false);
                viewShown = false;
            }
        }
    });
    viewShown = true;
    showKeyboard();
}

public String actionClick() {
    if (!viewShown) {
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        searchEditText.requestFocus();
        showKeyboard();
        viewShown = true;
        return null;
    } else {
        return getSearchText();
    }
}

public void showKeyboard() {
    inputMethodManager.showSoftInput(searchEditText, InputMethodManager.SHOW_IMPLICIT);
}

public void hideKeyboard() {
    inputMethodManager.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
}

public String getSearchText() {
    return searchEditText.getText().toString();
} }

搜索的相应xml布局:

<?xml version="1.0" encoding="utf-8"?>

<za.co.android.CustomAutoCompleteView android:id="@+id/edit_text_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/empty_layout"
    android:paddingBottom="8dp"
    android:textColor="@android:color/white"
    android:singleLine="true"
    android:imeOptions="actionSearch"
    android:textCursorDrawable="@null"
    android:paddingRight="36dp"
    android:layout_alignParentLeft="true"
    android:inputType="text"
    android:hint="@string/search"
    android:textColorHint="@color/palette_primary_light_grey"/>

<ImageButton
    android:id="@+id/image_button_search_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:drawable/ic_menu_close_clear_cancel"
    android:layout_toLeftOf="@+id/empty_layout"
    android:layout_centerVertical="true"
    android:layout_marginRight="4dp"/>

<FrameLayout
    android:id="@+id/empty_layout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_alignParentRight="true">
</FrameLayout>

自定义自动填充小部件代码:

public class CustomAutoCompleteView extends AutoCompleteTextView {

public CustomAutoCompleteView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public CustomAutoCompleteView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public CustomAutoCompleteView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

// this is how to disable AutoCompleteTextView filter
@Override
protected void performFiltering(final CharSequence text, final int keyCode) {
    String filterText = "";
    super.performFiltering(filterText, keyCode);
}

/*
 * after a selection we have to capture the new value and append to the existing text
 */
@Override
protected void replaceText(final CharSequence text) {
    super.replaceText(text);
}}

使用CustomViewSearch小部件:

将搜索图标添加到Activity的menu.xml中。然后在onOptionsItemSelected方法中 - 单击菜单项时,调用以下函数:

public void triggerSearch() {
    if (customViewSearch == null) {
        customViewSearch = new CustomViewSearch(this, null);
        customViewSearch.setOnEditorActionSearchListener(new CustomViewSearch.OnEditorActionSearchListener() {
            @Override
            public void onEditorActionSearch(String searchText) {
                // DO SOME STUFF
                ((AppItemListFragment_) getSupportFragmentManager()
                        .findFragmentById(R.id.appitem_list)).searchTriggered(searchText);
            }

            @Override
            public void onTextChangedListener(String text) {
                ((AppItemListFragment_) getSupportFragmentManager()
                        .findFragmentById(R.id.appitem_list)).searchTriggered(text);

            }

            @Override
            public List<String> getNewItemsForSuggestions(String text) {
                return getNewAutoCompleteStrings(text);
            }
        });

    } else {
        String searchText = customViewSearch.actionClick();
        if (searchText != null) {
            // DO SOME STUFF
            ((AppItemListFragment_) getSupportFragmentManager()
                    .findFragmentById(R.id.appitem_list)).searchTriggered(searchText);
        }
    }}

我希望这会有所帮助 - 对我来说,实现自定义搜索要比使用内置搜索更容易,并为您所在的任何屏幕提供范围搜索。