Android:自动填充TextView类似于Facebook应用程序

Sid*_*ele 29 android autocompletetextview android-listview

我的应用程序中有一个AutoCompleteTextView.该应用程序使用Facebook SDK.我按照SO:https://stackoverflow.com/a/12363961/450534这个问题的代码跟踪了点,并且有一个功能正常的搜索(过滤)活动.

现在,如何在键入@符号后获得类似Facebook App的内联自动完成功能,并且仍然保留用户键入的其他文本?有问题的活动是针对状态更新,可以包含用户的好友以及其他文本.的AutoCompleteTextViewEditText自然会多行之一.有关状态更新,请参阅.

我知道由于这篇文章中没有任何代码,我可能会被投票或甚至关闭问题.但它确实是一个标准的锅炉板列表过滤代码.

编辑:Android 的FriendCaster应用程序也是如此.

Hei*_*sch 35

首先将EditText变为MultiAutoCompleteTextView.MultiAutoCompleteTextView允许您替换文本的某些部分,例如"@"之后的文本.

你可以这样做:

final MultiAutoCompleteTextView inputEditText = (MultiAutoCompleteTextView) dialog.findViewById(R.id.MyEditText);

String[] COUNTRIES = new String[] { "Belgium", "France", "Italy", "Germany", "Spain" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
inputEditText.setAdapter(adapter);
inputEditText.setThreshold(1); //Set number of characters before the dropdown should be shown

//Create a new Tokenizer which will get text after '@' and terminate on ' '
inputEditText.setTokenizer(new Tokenizer() {

  @Override
  public CharSequence terminateToken(CharSequence text) {
    int i = text.length();

    while (i > 0 && text.charAt(i - 1) == ' ') {
      i--;
    }

    if (i > 0 && text.charAt(i - 1) == ' ') {
      return text;
    } else {
      if (text instanceof Spanned) {
        SpannableString sp = new SpannableString(text + " ");
        TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);
        return sp;
      } else {
        return text + " ";
      }
    }
  }

  @Override
  public int findTokenStart(CharSequence text, int cursor) {
    int i = cursor;

    while (i > 0 && text.charAt(i - 1) != '@') {
      i--;
    }

    //Check if token really started with @, else we don't have a valid token
    if (i < 1 || text.charAt(i - 1) != '@') {
      return cursor;
    }

    return i;
  }

  @Override
  public int findTokenEnd(CharSequence text, int cursor) {
    int i = cursor;
    int len = text.length();

    while (i < len) {
      if (text.charAt(i) == ' ') {
        return i;
      } else {
        i++;
      }
    }

    return len;
  }
});
Run Code Online (Sandbox Code Playgroud)

与此相关的一个"问题"是弹出窗口将显示在EditText视图下.要将其向上移动并将其放在当前编写的文本下,您可以执行以下操作:

inputEditText.addTextChangedListener(new TextWatcher() {

  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count) {
    Layout layout = inputEditText.getLayout();
    int pos = inputEditText.getSelectionStart();
    int line = layout.getLineForOffset(pos);
    int baseline = layout.getLineBaseline(line);

    int bottom = inputEditText.getHeight();

    inputEditText.setDropDownVerticalOffset(baseline - bottom);

  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  }

  @Override
  public void afterTextChanged(Editable s) {
  }
});
Run Code Online (Sandbox Code Playgroud)

注意:如果edittext中的行数多于edittext可以显示的行数,则当前不会处理下拉列表位置.