如何在单击搜索按钮后将搜索视图中输入的文本作为字符串发送到函数?

时间:2018-02-23 10:37:03

标签: android android-search

这是一个XML文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.thinkx.thinkx.friends"
android:background="#84cae6">
<SearchView
    android:id="@+id/search"
    android:queryHint="  Search ...."
    android:background="@drawable/rounded_edittext"
    android:layout_width="match_parent"
    android:layout_height="40dp">
</SearchView>
<ProgressBar
    android:id="@+id/progressBar2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:visibility="gone"/>
<android.support.v7.widget.RecyclerView
    android:layout_marginTop="50dp"
    android:id="@+id/recyclerView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="60dp"/>
</android.support.constraint.ConstraintLayout>

现在我想通过替换进度条在此回收站视图中显示文本。 我已经在searchview上看到了android文档,并且还遇到了很多问题,但它对我没有用!!

2 个答案:

答案 0 :(得分:1)

使用以下代码:

没有搜索按钮:当我的角色在TextWatcher / SearchView上发生变化时我会使用EditText方法:

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

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if(editTextSearch.getText().length()>=1){

             callJsonRequest(editTextSearch.getText().toString());  // 

        }
    });

使用搜索按钮:

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideKeyboard();
                if (utils.haveNetworkConnection()) {
                    if (validationDetails()) {
                        String searchText= editTextSearch.getText().toString();
                        callJsonRequest(searchText);
                      }   
            }
        });




public void callJsonRequest(String text){

}

答案 1 :(得分:0)

将此添加到您的代码中:

search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // Do something when Search icon is clicked on Keyboard, will occur only when Search Icon is pressed
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                // do something on text change, it will occur on a single change
                    return false;
            }
        });