简体   繁体   English

如何以编程方式在searchview中提交文本?

[英]how to submit text in searchview programmatically?

How can I submit text inside a SearchView programmatically? 如何以编程方式在SearchView中提交文本?

I get text from previous activity and put it in the SearchView in the 2nd activity. 我从之前的活动中获取文本并将其放在第二个活动的SearchView中。

    // get text from previous activity
    schoolDbn = getIntent().getExtras().getString("dbn");

    final SearchView searchView = findViewById(R.id.searchID);


    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() 
     {
        @Override
        public boolean onQueryTextSubmit(String s) {
            myadapter.getFilter().filter(s);
            return true;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            myadapter.getFilter().filter(s);
            return true;
        }
    });

    // sets text from first activity
    searchView.setQuery(schoolDbn, true);

    }

SearchView XML SearchView XML

    <android.support.v7.widget.SearchView
    android:id="@+id/searchID"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    </android.support.v7.widget.SearchView>

What I want to happen User clicks item in first activity and in 2nd activity SearchView auto submits text. 我想要发生什么用户点击第一个活动和第二个活动中的项目SearchView自动提交文本。

When you clear focus from searchView the onQueryTextSubmit(String s) method not called. 当您从searchView清除焦点时,未调用onQueryTextSubmit(String s)方法。 So remove this line searchView.clearFocus(); 所以删除这行searchView.clearFocus(); after setQuery(); setQuery(); and make sure set setOnQueryTextListener before setQuery(); 并确保在setQuery();之前设置setOnQueryTextListener setQuery();

And

For hiding keyboard when the activity starts use android:windowSoftInputMode="stateHidden" in your activity tag in your AndroidManifest 为了在activity开始时隐藏键盘,请在AndroidManifest activity标签中使用android:windowSoftInputMode="stateHidden"

Or 要么

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​; on your activity's onCreate method. 在你的活动的onCreate方法上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM