4

当用户编辑文本时,我必须显示最近输入的文本的下拉列表。示例是登录页面显示以前登录的用户

4

4 回答 4

4

您正在寻找 AutoCompleteTextView http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

创建登录列表

当用户登录时,您需要将该登录信息保存到某种持久性存储(数据库、文本文件)中。

创建自动完成列表

每次使用 EditText 登录创建表单时

  1. 从数据库中提取以前的登录值
  2. 从之前的登录值中创建一个字符串数组
  3. 从 String 数组中创建一个数组适配器
  4. 将数组适配器附加到您的 AutoCompleteTextView。
于 2012-05-08T13:46:05.353 回答
4

我只是在找不到解决方案后才实现了这一点。我做了 Gopher 所说的,但将其存储为私人偏好而不是文件。我还添加了一个 if 语句来阻止该项目两次出现在列表中。

public void YOURSUBMITBUTTON(View view) 
{
    PrevItemsAutoComplete customitemname = (PrevItemsAutoComplete) findViewById(R.id.YOURTEXTVIEW);

    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
    editor.putString("customitemname",customitemname.getText().toString());
    editor.commit();

    customitemname.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, updatedropdown(5)));
}

public String[] updatedropdown(int listlength)
{
    boolean itemalreadyinlist=false;
    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();

    for(int i = 0; i<listlength; i++)
    {
        if (getPreferences(MODE_PRIVATE).getString("customitemname","").equals(getPreferences(MODE_PRIVATE).getString("recentlistitem"+String.valueOf(i),"")))
        {
            itemalreadyinlist=true;
            for(int j = i; j>0; j--)
            {
                editor.putString("recentlistitem"+String.valueOf(j),getPreferences(MODE_PRIVATE).getString("recentlistitem"+String.valueOf(j-1),""));
            }
            editor.putString("recentlistitem0",getPreferences(MODE_PRIVATE).getString("customitemname",""));
            editor.commit();
            break;
        }
    }

    if( !itemalreadyinlist)
    {
        for(int i = listlength-1; i>0; i--)
        {
            editor.putString("recentlistitem"+String.valueOf(i),getPreferences(MODE_PRIVATE).getString("recentlistitem"+String.valueOf(i-1),""));
        }
        editor.putString("recentlistitem0",getPreferences(MODE_PRIVATE).getString("customitemname",""));
        editor.commit();
    }

    String[] recentlist = new String[listlength];
    for(int i=0;i<listlength;i++)
    {
        recentlist[i] = getPreferences(MODE_PRIVATE).getString("recentlistitem"+String.valueOf(i),"");
    }

    return recentlist;
}

其中 customitemname 是包含输入的文本视图。单击按钮时会调用上述代码。

如果你想知道 PrevItemsAutoComplete 是什么,它是一个扩展 AutoCompleteTextView 的自定义类。虽然它适用于后者,但我更喜欢前者。这是课程:

import android.content.Context;  
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;

public class PrevItemsAutoComplete extends AutoCompleteTextView {

public PrevItemsAutoComplete(Context context) {
    super(context);
}

public PrevItemsAutoComplete(Context arg0, AttributeSet arg1) {
    super(arg0, arg1);
}

public PrevItemsAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
    super(arg0, arg1, arg2);
}

@Override
public boolean enoughToFilter() {
    return true;
}

@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
    if (focused){
        setText("");
        performFiltering("", 0);
        showDropDown();
    }
}

}

PrevItemsAutoComplete 是从此处的原始版本修改而来的:https ://stackoverflow.com/a/5783983/2066079 。

如果您选择使用此类,请记住在 xml 中使用<your.namespace.PrevItemsAutoComplete ... />而不是。<AutoCompleteTextView ... />

此外,您可能必须添加customitemname.addTextChangedListener(this);到您的 onCreate。


我花了很多时间在这上面,所以如果这对你有帮助,请不要忘记点赞。

于 2013-03-12T17:29:12.233 回答
1

使用自动完成文本视图

于 2012-05-08T13:40:43.043 回答
0

您可以使用AutoComplete TextView。但是您需要进行更改,就像您必须维护以前登录用户的 ArrayList 一样。

于 2012-05-08T13:42:36.997 回答