如何在android工具栏中添加按钮到搜索视图?

时间:2015-09-01 17:21:24

标签: android

现在我有下一个代码:

open(my $old, '<', 'report.txt') or die $!;
open(my $new, '>', 'output.txt') or die $!;

while (<$old>) {
    chomp;
    my @parts = split(/\s+/);
    print $new "$parts[0] ===> $parts[3]\n";
}

close($old);
close($new);

我想附加到搜索视图按钮搜索下一个并搜索上一个(在WebView中搜索)。即G。我希望得到这个(只有当searchView展开时才会出现绘制的箭头):example

我该怎么做?

2 个答案:

答案 0 :(得分:6)

所以,我在我的活动中通过这段代码解决了它:

@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>

答案 1 :(得分:1)

另一种解决方案:

  public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.artykul_search_menu, menu);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        MenuItem searchItem = menu.findItem(R.id.artykul_search_menu);


        nextItem = menu.add(0, 1,1,"next");
        nextItem.setIcon(R.drawable.baseline_expand_more_black_24);
        nextItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        nextItem.setVisible(false);

        prevItem = menu.add(0, 2,2,"prev");
        prevItem.setIcon(R.drawable.baseline_expand_less_black_24);
        prevItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        prevItem.setVisible(false);


        searchView = (SearchView) searchItem.getActionView();
        searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                nextItem.setVisible(true);
                prevItem.setVisible(true);
            }
        });

        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                nextItem.setVisible(false);
                prevItem.setVisible(false);
                return false;
            }
        });