如何使搜索活动在每个字符输入时都命中 API - AndroidX
Posted
技术标签:
【中文标题】如何使搜索活动在每个字符输入时都命中 API - AndroidX【英文标题】:How to make Search Activity to hit API every character input - AndroidX 【发布时间】:2020-03-15 22:07:15 【问题描述】:我想制作类似于每次输入字符时都可以访问 API 的 Activity
SearchActivity.java
public class SearchActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Intent intent = getIntent();
handleIntent(intent);
// Handles the intent that carries user's choice in the Search Interface
private void handleIntent(Intent intent)
if (Intent.ACTION_SEARCH.equals(intent.getAction()))
String query = intent.getStringExtra(SearchManager.QUERY);
Log.i("Main", "Received query: " + query);
else if (Intent.ACTION_VIEW.equals(intent.getAction()))
Bundle bundle = this.getIntent().getExtras();
assert (bundle != null);
if (bundle != null)
// Menu callbacks ______________________________________________________________________________
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.menu_searchactivity, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
int id = item.getItemId();
if (id == R.id.action_settings)
return true;
if (id == R.id.action_search)
// Calls Custom Searchable Activity
Intent intent = new Intent(this, SearchActivity.class);
startActivity(intent);
return true;
return super.onOptionsItemSelected(item);
activity_search.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/RecyclerView_SearchView"
android:layout_
android:layout_/>
</RelativeLayout>
menu_searchactivity.xml > 工具栏菜单
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="edsilfer.com.br.edsilfer.Main">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="asdasdasdasd"
tools:ignore="AppCompatResource" />
<item
android:id="@+id/action_search"
android:orderInCategory="200"
android:title="Search"
android:icon="@drawable/search"
app:showAsAction="ifRoom"/>
</menu>
我不确定如何进行这样的工具栏设计 你的经验对我有很大帮助 ..................................................... .............................................
【问题讨论】:
【参考方案1】:youreditext.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
// your logic
@Override
public void afterTextChanged(Editable editable)
);
【讨论】:
【参考方案2】:正如您的问题所描述的,Text watcher 会帮助您。
在 afterTextChange 中实现调用 API 是个好办法。
【讨论】:
以上是关于如何使搜索活动在每个字符输入时都命中 API - AndroidX的主要内容,如果未能解决你的问题,请参考以下文章
如何使 SearchList 在 Android 中按字符串工作?
如何将它们添加到字典中,并在python中每5个循环后使活动变量成为结尾?