单击搜索按钮后,如何将在搜索视图中输入的文本作为字符串发送到函数?
How can i send text entered in a search view to a function as a string after the search button is clicked?
这是一个 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 文档,也搜索了很多问题,但它对我不起作用!!
使用下面的代码:
没有搜索按钮: 我在这里使用 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){
}
将此添加到您的代码中:
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;
}
});
这是一个 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 文档,也搜索了很多问题,但它对我不起作用!!
使用下面的代码:
没有搜索按钮: 我在这里使用 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){
}
将此添加到您的代码中:
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;
}
});