Có thanh công cụ trong hoạt động của mình, tôi đã cố gắng thêm Chế độ xem tìm kiếm từ Menu nhưng không thể thêm thanh công cụ Tìm kiếm trong thanh công cụ, Cách thêm Chế độ xem tìm kiếm trong thanh công cụ hoạt động của tôi.Android SearchView trong Thanh công cụ
10
A
Trả lời
28
tôi đã thực hiện với Fragment bằng cách này.
tôi đã làm với onTextChangeListener của xem tìm kiếm, chỉ cần kiểm tra trong bạn Logcat.
Ảnh chụp màn hình tham khảo:
menu.xml - (res
->menu
)
<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=".HomeActivity">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
MainFragment.java
public class MainFragment extends Fragment {
private SearchView searchView = null;
private SearchView.OnQueryTextListener queryTextListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
Log.i("onQueryTextChange", newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
Log.i("onQueryTextSubmit", query);
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
}
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
// Not implemented here
return false;
default:
break;
}
searchView.setOnQueryTextListener(queryTextListener);
return super.onOptionsItemSelected(item);
}
}
Xong
3
menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
\ res \ xml \ searchable.xml:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" />
AndroidManifest.xml:
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light">
<!-- Customize your theme here. -->
</style>
MainActivity.java:
package com.example.searchview;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return super.onOptionsItemSelected(item);
}
}
Các vấn đề liên quan
- 1. Android - SearchView trên thanh công cụ không mở rộng
- 2. Thanh công cụ với bộ lọc tạm thời SearchView RecyclerView
- 3. Có thể căn chỉnh SearchView trong AppCompat 21 bằng thanh công cụ không?
- 4. Thay đổi màu SearchView của biểu tượng trở lại và rõ ràng trong Thanh công cụ Android
- 5. android thanh công cụ popupTheme vs theme
- 6. Thanh công cụ trong DialogFragment
- 7. Android: Thanh trạng thái thanh công cụ chồng chéo
- 8. Thanh công cụ thu gọn - thanh công cụ trong thanh trạng thái
- 9. Cách lấy lại thanh công cụ trong Android Studio IDE?
- 10. Biểu tượng tùy chỉnh trong thanh công cụ Android
- 11. Tạo thanh công cụ tùy chỉnh trong android
- 12. setSupportActionBar thanh công cụ lỗi
- 13. Thanh công cụ không hiển thị từ bên trong thanh công cụ thu gọn
- 14. Android SearchView chuỗi trống
- 15. Cách thêm Thanh công cụ trong PreferenceActivity
- 16. ActionBar vs Thanh công cụ hoặc ActionBar và Thanh công cụ
- 17. Android ActionbarSherlock SearchView
- 18. android: fitsSystemWindows không hoạt động trên Thanh công cụ
- 19. Chiều cao của Thanh công cụ Android là gì?
- 20. Thanh công cụ của Android sụp đổBạn gặp vấn đề
- 21. Android SearchView onclick
- 22. Android BottomSheet: Sản phẩm hinding dưới thanh công cụ
- 23. SearchView is null android
- 24. Thanh công cụ Android với cả nút home và back
- 25. Biểu tượng thanh công cụ tô màu trên Android
- 26. Cách áp dụng chủ đề cho Thanh công cụ Android?
- 27. Thanh công cụ Android không mở rộng khi cuộn lên
- 28. Thanh công cụ Android quá cao ở chế độ ngang
- 29. Phong cách thanh công cụ của Android Spinner
- 30. Làm cách nào để tạo thanh công cụ trông giống như thanh công cụ trong Keynote?
Hey! Tôi đang sử dụng mã của bạn nhưng nút tìm kiếm của tôi không mở rộng/hiển thị trường văn bản tìm kiếm, bất kỳ ý tưởng nào? –
@LucaNicoletti vì bạn quên thêm inflater vào 'OnCreateView()' của bạn, hãy kiểm tra này 'return inflater.inflate (R.layout.fragment_main, container, false);' –
Không hoạt động cho tôi! Chỉ có cách để thực hiện việc này trong trường hợp của tôi là với giao diện: https://stackoverflow.com/a/37965747/1259763. –