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

时间:2017-07-08 10:18:11

标签: android searchactivity

我按照link来创建搜索界面。 但是没有关于如何在点击编辑文本时开始搜索活动的正确解释,但只有菜单项的解释。 我的界面在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时触发搜索的代码

1 个答案:

答案 0 :(得分:0)

终于找到了答案

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

触发搜索活动

查看here详细说明