[英]How to open new activity by clicking on SearchView
I created an Android project in which I use search view.我创建了一个使用搜索视图的 Android 项目。 I want to open a new activity by clicking the SearchView.
我想通过单击 SearchView 打开一个新活动。 When I click icon of SearchView it work perfectly, but when I click in the empty space of the SearchView the keyboard pops up and the new activity is not opening up.
当我单击 SearchView 的图标时,它可以正常工作,但是当我单击 SearchView 的空白区域时,键盘会弹出并且新活动没有打开。
My java code:我的Java代码:
searchView=root.findViewById(R.id.search);
searchView.setIconifiedByDefault(true);
searchView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent searchIntent=new Intent(getActivity(),Notification_activity.class);
getActivity().startActivity(searchIntent);
}
});
My XML:我的 XML:
<SearchView
android:id="@+id/search"
android:layout_width="333dp"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/search_round_corner"
android:iconifiedByDefault="false"
android:queryHint="Enter text to search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</SearchView>
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(this,page,class);
startActivity(i);
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
//do what you want searchview is not expanded
return false;
}
});
you should use the query listener for this purpose because after type single word then it will goes to the second activity when you submit the button from keyboard and perform search view functionality.您应该为此目的使用查询侦听器,因为在键入单个单词后,当您从键盘提交按钮并执行搜索视图功能时,它将转到第二个活动。 here is the code
这是代码
searchView.OnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
String text = s;
if (text.length() > 0) {
startActivity(new Intent(Class A Context, Sceond Class B));
}
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
use this code,使用此代码,
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(A.this, B.class));
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.