我创建了一个使用搜索视图的Android项目。我想通过单击"搜索视图"打开一个新活动。当我点击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;
}
});
您应该为此目的使用查询侦听器,因为在键入单个单词后,当您从键盘提交按钮并执行搜索视图功能时,它将进入第二个活动。这是代码
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;
}
});
使用此代码,
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(A.this, B.class));
}
});