【问题标题】:SearchView widget - any way to SELECT ALL text inside?SearchView 小部件 - 有什么方法可以选择里面的所有文本?
【发布时间】:2014-04-04 17:20:54
【问题描述】:

我正在使用 SearchView 小部件(Honeycomb 中的新功能)。我可以通过 setQuery 设置初始文本,这很有效。

但是有没有办法全选里面的文本(类似于EditText.selectAll)?

我想为用户提供先前搜索的预设文本,但无需删除旧文本即可轻松输入新文本。

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    你只需要捕获内部的 EditText,然后做任何你想做的事情。

    EditText edit = (EditText)searchView.findViewById(R.id.search_src_text);
    edit.selectAll();
    

    【讨论】:

      【解决方案2】:

      有点晚了,但我遇到了类似的问题。

      我想为用户提供先前搜索的预设文本,但无需删除旧文本即可轻松输入新文本。

      我找到了适合我的解决方案。您只需要访问内部的 EditText,然后以通常的方式选择所有文本。

      为此,请尝试以下操作。 请注意,我的代码是在 c# 中,而不是在 Java 中,但类似。

      int id = searchView.Context.Resources.GetIdentifier("android:id/search_src_text", null, null);
      EditText editText = searchView.FindViewById<EditText>(id);
      editText.SelectAll();
      

      在 Java 中应该是这样的:

      int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
      EditText editText = (EditText) searchView.findViewById(id);
      editText.selectAll();
      

      我刚刚在 API 级别 15 上对其进行了测试!但我认为它也适用于较低级别。

      【讨论】:

        【解决方案3】:

        您想做的事情在 PC 上运行良好,但在触控设备上不太方便。

        我会使用suggestion provider 允许用户重复使用最近的搜索,可能启用查询优化。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-15
          • 1970-01-01
          • 1970-01-01
          • 2018-12-22
          • 1970-01-01
          相关资源
          最近更新 更多