我一直在在Custom search
应用程序栏中实施Android
。因此,我在menu_main.xml
中添加了以下代码
<item android:id="@+id/action_search"
android:title="search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
android:hint="Enter Tags"
app:actionViewClass="android.support.v7.widget.SearchView" />
现在,当我查看
HomeActivity
时。看起来像这样:很好!我还没有实现上述
SearchView
的后端代码。现在,我想在Search bar
上方添加以下一些功能用户可以使用标签进行搜索(使用标签进行搜索意味着如果两个单词之间有空格,那么我会将它们视为标签)。它与
Pintreset
应用程序非常相似。此外,我想从
Search Bar
获取这些标签并将其放入Get
请求参数。屏幕截图
问题
现在,忘记
Style
。我只想知道如何在Search Bar
中实现此目标?如何在每个带有
Android app bar
叉号的标签上添加框?任何帮助将是可观的。
编辑1
我正在添加
onCreateOptionsMenu
方法:@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
if (null != searchView) {
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
return true;
}
public boolean onQueryTextSubmit(String query) {
//Here u can get the value "query" which is entered in the search box.
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
我正在使用
onCreateOptionsMenu.onQueryTextSubmit
方法提交文本。我现在应该怎么办? 最佳答案
您应该使用自定义搜索视图才能访问工具栏中的EditText(菜单布局中的app:actionViewClass
属性)。设置TextWathcer
并管理afterTextChanged
回调内的跨度之后。
我写了一些具有基本背景跨度实现的示例。这是解决您的问题的主要概念和起点。
请参见下面的示例:
menu_main.xml:
<item android:id="@+id/action_search"
android:title="search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
android:hint="Enter Tags"
app:actionViewClass="your.package.CustomSearchView" />
CustomSearchView.java:
public class CustomSearchView extends SearchView {
private AutoCompleteTextView mSearchAutoComplete;
public CustomSearchView(Context context) {
super(context);
initialize();
}
public CustomSearchView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public CustomSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}
public void initialize() {
mSearchAutoComplete = (AutoCompleteTextView) findViewById(android.support.v7.appcompat.R.id.search_src_text);
if (mSearchAutoComplete == null) {
Log.wtf("TEST", "Some Changes in AppCompat????");
return;
}
mSearchAutoComplete.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
mSearchAutoComplete.removeTextChangedListener(this);
setSpans(s, Color.RED);
mSearchAutoComplete.addTextChangedListener(this);
}
});
}
private void setSpans(Editable s, @ColorInt int backgroundColor) {
BackgroundColorSpan[] spans = s.getSpans(0, s.length(), BackgroundColorSpan.class);
String[] words;
if (s.toString().endsWith(" ")) {
words = (s.toString() + "X").split("\\s");
} else {
words = s.toString().split("\\s");
}
int completedWordsCount = words.length - 1;
if (spans.length != completedWordsCount) {
for (BackgroundColorSpan span : spans) {
s.removeSpan(span);
}
int currentIndex = 0;
for (int i = 0; i < words.length - 1; i++) {
s.setSpan(new BackgroundColorSpan(backgroundColor), currentIndex, currentIndex + words[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
currentIndex += words[i].length() + 1;
}
}
}
}
附言该链接可能对您也很有用,以便设置可点击范围-Link。