单击编辑文本时如何启动 SearchActivity

How to start SearchActivity when clicked on edit text

我按照这个 link 创建搜索界面。 但是没有关于如何在单击编辑文本时开始搜索 activity 的正确解释,但只有菜单项有解释。 我的界面在 NoActionBarStyle 中,如下面的代码和图片所示

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="perspective.secretbrowser.MainActivity">

    <android.support.constraint.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="56dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/colorPrimary"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintLeft_creator="1"
        android:id="@+id/constraintLayout">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@drawable/menu_small"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:clickable="true"
            android:id="@+id/options"
            android:visibility="visible"
            android:layout_marginRight="8dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp" />

        <ImageView
            android:id="@+id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/tabs_small"
            android:visibility="visible"
            android:scaleType="fitXY"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:clickable="true"
            app:layout_constraintRight_toLeftOf="@+id/options"
            android:layout_marginRight="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp" />

        <EditText
            android:id="@+id/url_txt"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/url_bg"
            android:hint="@string/search_hint"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/tabs"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.333"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp" />
    </android.support.constraint.ConstraintLayout>

    <com.gc.materialdesign.views.ProgressBarDeterminate
        android:id="@+id/progressBar"
        android:layout_width="0dp"
        android:layout_height="3dp"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/webView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout" />

    <WebView
        android:id="@+id/webView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/progressBar" />
</android.support.constraint.ConstraintLayout>

MainActivity.xml

建议我点击 edittext

时将触发搜索的代码activity

终于找到答案

edittext 上设置 onclicklistner 并使用 search(null,false,null,false)

触发搜索 activity

查看here了解详细说明