简体   繁体   English

如何通过单击 SearchView 打开新活动

[英]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.

相关问题 如何通过单击片段中的卡片视图来打开新活动? - how to open new activity by clicking card view in fragments? 如何通过单击 Android 中的导航抽屉项打开新活动 - How to open New Activity by clicking Navigation Drawer Item in Android 单击活动的其他位置时如何使 SearchView 失去焦点并崩溃 - How to make SearchView lose focus and collapse when clicking elsewhere on activity 无法在点击卡片时打开新活动 - Unable to open new activity on clicking card 单击按钮后打开一个新活动 - Open a new activity after clicking the Button 如何打开新活动并通过单击Firebase通知在textview上打印通知消息 - How open a new activity & print the notification message on a textview by clicking on firebase notification 如何通过单击片段XML文件上的按钮从片段Java文件中打开新的Activity? - How to open new Activity from a fragment java file by clicking the button on the fragment xml file? 单击带有搜索栏元素的列表视图后如何打开新活动? - How to open new activity after clicking list view with search bar element? 如何通过单击通知打开带有进度的活动 - How to open an activity with its progress by clicking on the notification 单击菜单项后如何打开活动? - How to open an activity after clicking on a menu item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM