【问题标题】:how to get the text of a edit text when after pressing a key按下键后如何获取编辑文本的文本
【发布时间】:2015-04-22 14:05:27
【问题描述】:

当用户在 EditText 中键入文本时,我想搜索 ListView。按下一个键后,我需要获取输入的文本部分才能开始搜索。那么有什么我可以使用的方法吗?非常感谢简单的帮助。

【问题讨论】:

    标签: android android-listview textview keylistener android-event


    【解决方案1】:

    如果您使用ArrayAdapter 来填充您的ListView,那么可以这样操作:

       edittext.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                yourAdapter.getFilter().filter(s);
        }
    
        @Override
        public void afterTextChanged(Editable s) {
    
        }
    });
    

    TextWatch 添加到您的EditText 并在您的适配器中使用filter 以显示匹配的结果。要突出显示匹配的结果,您可能需要使用SpannableString 来“美化”它。

    来自ArrayAdapter.getFilter()

    返回一个过滤器,可用于通过过滤模式约束数据。

    此方法通常由 Adapter 类实现。

    【讨论】:

      【解决方案2】:

      你可以在你的activity中实现TextWatcher,看看:

      http://developer.android.com/reference/android/text/TextWatcher.html

      【讨论】:

        【解决方案3】:

        假设你有一个视图 xml

        <EditText
          android:id="@+id/search"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:inputType="text"
          android:imeOptions="actionSend" />
        

        您在控制器中的代码将是

          final EditText searchField = (ListView)result.findViewById(R.id.search);;
          searchField.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        
              }
        
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
        
              }
        
              @Override
              public void afterTextChanged(Editable s) {
                  //do search is the method where you will delegate the search process.
                  doSearch(searchField.getText().toString());
              }
          });
        

        【讨论】:

          【解决方案4】:

          你必须为你的编辑文本设置一个 TextWatcher

          edittext.addTextChangedListener(new TextWatcher() {
                  @Override
                  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          
                  }
          
                  @Override
                  public void onTextChanged(CharSequence s, int start, int before, int count) {
          
                  }
          
                  @Override
                  public void afterTextChanged(Editable s) {
                       String newText = s.toString();
                  }
              });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-07-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-01-10
            • 2013-10-25
            • 1970-01-01
            相关资源
            最近更新 更多