【问题标题】:Make EditText offer results for searching使 EditText 提供搜索结果
【发布时间】:2020-01-30 22:07:56
【问题描述】:

我用 ImageButton 创建了以下 EditText:

通过使用以下代码:

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_percent="0.12">

    <EditText
        android:id="@+id/txt_search"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="8dp"
        android:paddingLeft="24dp"
        android:textSize="14sp"
        android:background="@drawable/et_rounded"
        android:hint="Search for book or author"
        android:textColorHint="@color/colorGray"
        app:layout_constraintTop_toTopOf="parent"/>

    <ImageButton
        android:id="@+id/btn_search"
        android:layout_width="48dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="24dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="8dp"
        android:layout_alignRight="@id/txt_search"
        android:background="@android:color/transparent"
        android:src="@drawable/ic_search_black_24dp"/>

</RelativeLayout>

我想得到两件事:

1) 是否有一个选项可以在键盘中添加一个搜索按钮,一旦我单击此按钮,它将搜索结果,就像我在某些网站中搜索时一样:

2) 有没有办法制作弹出菜单之类的东西,一旦我输入编辑文本,它会提供很少的搜索结果?

谢谢

【问题讨论】:

  • 1) 检查stackoverflow.com/a/3205405/10883621 .. 2) 你的意思是像 autoCompleteTextView(developer.android.com/reference/android/widget/…) ,还是你想从某个地方实时搜索(iE 从数据库)?
  • 第一个问题完成了,谢谢。关于第二,我确实想要一种自动完成,但不是基于我手动插入的一些数据,而是通过一些谷歌搜索或类似的有很多结果的东西。例如,如果我输入 harry p,我希望它为哈利波特提供结果

标签: android search android-edittext


【解决方案1】:

第一个

在edittex中添加如下属性(我的代码示例是kotlin)

android:imeOptions="actionSearch"

要捕获事件,请执行以下代码:

yourTextView.setOnEditorActionListener{ _,action,_->
            if (action == EditorInfo.IME_ACTION_SEARCH){
                //put your logic here
                return@setOnEditorActionListener true
            }
            return@setOnEditorActionListener false
        }

第二次,看看autoCompleteExample

【讨论】:

    【解决方案2】:

    回答你的两个问题:

    首先指定 inputType 和 ime 选项:

     <AutoCompleteTextView
            android:id="@+id/autocomplete"
            android:imeOptions="actionSearch"
            android:inputType="text" />
    

    然后注册适配器:

    // field
    val adapter by lazy { ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line) }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    
        autocomplete.setAdapter(adapter)
        autocomplete.doOnTextChanged { text, _, _, _ ->
            // Call api to find search results for your text:
            callApiFor(text.toString())
        }
    
        autocomplete.setOnEditorActionListener { textView, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // call an API to find your results
                searchResults(textView.text.toString())
                return@setOnEditorActionListener true
            }
    
            false
        }
    }
    

    收到数据后:

    adapter.clear()
    adapter.addAll(resultsList)
    adapter.notifyDataSetChanged()
    

    搜索建议链接: http://suggestqueries.google.com/complete/search?client=firefox&q=some%20query

    这里有一些文档(非官方,因为我找不到任何官方文档): https://shreyaschand.com/blog/2013/01/03/google-autocomplete-api/

    您将获得以下格式的结果:

    [
      "some query",
      [
        "some query",
        "some query in sql",
        "some query in mysql",
        "some query in oracle",
        "some_queryset",
        "queryselectorall some",
        "querylist some",
        "query some information",
        "query some data",
        "query some questions"
      ]
    ]
    

    和谷歌一样:

    【讨论】:

      猜你喜欢
      • 2020-03-05
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多