gpt4 book ai didi

android - 如何在android工具栏中添加按钮以搜索 View ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:35:48 27 4
gpt4 key购买 nike

现在我有下一个代码:

@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_toolbar_reader, menu);

MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if(null!=searchManager ) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}

我想附加到搜索 View 按钮搜索下一个和搜索上一个(在 Web View 中搜索)。 e. G。我想得到这个(绘制的箭头应该只在 searchView 未折叠时出现):example

我该怎么做?

最佳答案

所以,我在我的 Activity 中通过这段代码解决了这个问题:

@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_toolbar_reader, menu);


final MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();

LayoutParams navButtonsParams = new LayoutParams(toolbar.getHeight() * 2 / 3, toolbar.getHeight() * 2 / 3);

Button btnNext = new Button(this);
btnNext.setBackground(getResources().getDrawable(R.drawable.ic_keyboard_arrow_down_white_48pt_3x));

Button btnPrev = new Button(this);
btnPrev.setBackground(getResources().getDrawable(R.drawable.ic_keyboard_arrow_up_white_48pt_3x));

searchStats = new TextView(this);

((LinearLayout) searchView.getChildAt(0)).addView(searchStats);
((LinearLayout) searchView.getChildAt(0)).addView(btnPrev, navButtonsParams);
((LinearLayout) searchView.getChildAt(0)).addView(btnNext, navButtonsParams);

((LinearLayout) searchView.getChildAt(0)).setGravity(Gravity.BOTTOM);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
searchQuery = newText;
if (!newText.equals("")) {
lawTextWebView.findAllAsync(newText);
} else {
lawTextWebView.clearMatches();
}
return false;
}
});

btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!searchQuery.equals("")) {
lawTextWebView.findNext(true);
}
}
});

btnPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!searchQuery.equals("")) {
lawTextWebView.findNext(false);
}
}
});

return true;
}

并保存此操作栏标记 res/menu/menu_toolbar_reader.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/action_search"
android:title="@android:string/search_go"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
yourapp:showAsAction="always|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />

<item
android:id="@+id/action_contents"
android:title="@string/contents"
android:icon="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
yourapp:showAsAction="always" />
</menu>

关于android - 如何在android工具栏中添加按钮以搜索 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32337495/

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