gpt4 book ai didi

android - 使用工具栏中的搜索栏

转载 作者:行者123 更新时间:2023-11-30 02:04:39 25 4
gpt4 key购买 nike

我想在我的 Android 应用程序中使用搜索 View 。为此,我在我的 app_bar 中添加了搜索 View :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColorDark">
<SearchView
android:id="@+id/mySearchView"
android:icon="@drawable/ic_magnify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:iconifiedByDefault="true"
android:queryHint="Search from Qur'an"/>

</android.support.v7.widget.Toolbar>

现在,实现它的方法是:

 search = (SearchView) findViewById(R.id.mySearchView);
search.setQueryHint("SearchView");

//*** setOnQueryTextFocusChangeListener ***
search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), String.valueOf(hasFocus) +" I am in has focus",
Toast.LENGTH_SHORT).show();
}
});

//*** setOnQueryTextListener ***
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub

startActivity(new Intent(getApplicationContext(),SearchResultsActivity.class));

return false;
}

@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), newText,
Toast.LENGTH_SHORT).show();
return false;
}
});

现在,我想像这样添加下拉列表:

drop down

现在,点击这些项目中的任何一个,一个新的 Activity 应该开始。我怎样才能做到这一点。另外,我是 android 开发的新手,所以请建议我任何其他方法。

编辑 1:

在这里使用 setOnSuggestionListener 不起作用:

search.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int i) {
return true;
}

@Override
public boolean onSuggestionClick(int i) {
//your intent
startActivity(new Intent(getBaseContext(),SearchResultsActivity.class));
return true;
}
});

最佳答案

只需在 suggestionListener 上实现
下面的代码是简短的示例,可能会对您有所帮助

 searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int i) {
return true;
}

@Override
public boolean onSuggestionClick(int i) {
//your intent
return true;
}
});

关于android - 使用工具栏中的搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30883055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com