我创建了一个使用搜索视图的Android项目。我想通过单击SearchView打开一个新活动。当我单击SearchView图标时,它可以正常工作,但是当我单击SearchView的空白区域时,键盘会弹出,并且新活动不会打开。
我的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);
}
});
我的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;
}
});