我正在尝试使用链接 http://developer.android.com/guide/topics/search/search-dialog.html 在我的 Android 应用程序中实现搜索功能.
它只能搜索按下搜索按钮时输入的文本。但是当搜索栏中的文本发生变化时,它无法执行搜索。我想实现那种功能,就像在 Android 手机的“People”或“Contact”应用程序中一样。
我曾多次尝试搜索此内容,但找不到任何链接。如果有人可以将我指向这些链接,那将很有用。提前致谢。
编辑:
我仅在 MainActivity 中实现了搜索功能。 MainActivity.java 的代码:
findViewById(R.id.id1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onSearchRequested();
}
});
Android list :
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.SEARCH" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/activity_search" />
</activity>
最佳答案
如果您正在使用搜索栏,请执行此操作,
lv.setTextFilterEnabled(true);
但我建议您使用 EditText
而不是搜索栏,如果您同意对 EditText
执行此操作
edittext = (EditText) findViewById(R.id.edittext);
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
Search_Activity.this.adapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
希望对您有所帮助。
关于android - 在 Android 中实现文本更改的搜索功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35175079/