如何使搜索 Activity 命中 API 每个字符输入 - AndroidX

How to make Search Activity to hit API every character input - AndroidX

我想做类似这样的 Activity 每次我输入一个字符都可以命中 API

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_width="match_parent"
    android:layout_height="match_parent"

    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_width="match_parent"
        android:layout_height="wrap_content"/>
</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>

我不确定如何设计这样的工具栏 你的经验对我有很大帮助 ..................................................... .....................................

正如您的问题所述,Text watcher 会对您有所帮助。

在 afterTextChange 中实现调用 API 是个好方法。

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



        }
    });