在Android中自动完成Edittext

时间:2015-10-20 03:58:27

标签: java android

我想要输入msg以及在单个Edittext android中显示名称的autoCompletetext(我在数据库中拥有的内容)。可能吗 ? 例如: -

我的名字是 Amit 。我住在德里。 所以在上面的句子中,Amit应该来自我的数据库中的AutoComplete。其余的单词都是正常的。

1 个答案:

答案 0 :(得分:1)

您可以使用AutoCompleteTextView

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="ID:" />

        <AutoCompleteTextView
            android:id="@+id/autoID"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="PW:" />

        <AutoCompleteTextView
            android:id="@+id/autoPW"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:inputType="textPassword" />
    </TableRow>
</TableLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Submit" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel" />
</LinearLayout>

活动:

private Button btnSubmit, btnCancel;
private AutoCompleteTextView autoID, autoPW;
private ListView lvID;
private Button btnListID;
private EditText editID;

private ArrayList<String> arrID = new ArrayList<String>();
private ArrayAdapter<String> adapterID = null;

private ArrayList<String> arrPW = new ArrayList<String>();
private ArrayAdapter<String> adapterPW = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnSubmit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            addCache(autoID.getText().toString());
        }
    });
    btnCancel = (Button) findViewById(R.id.btnCancel);
    btnCancel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            autoID.setText("");
            autoPW.setText("");
        }
    });
    autoID = (AutoCompleteTextView) findViewById(R.id.autoID);
    autoPW = (AutoCompleteTextView) findViewById(R.id.autoPW);
    getCache();
}

private void getCache() {
    SharedPreferences prefs = getSharedPreferences("LOGINS", MODE_PRIVATE);
    int total_ID = prefs.getInt("COUNT_ID", 0);
    while (total_ID>0) {
        String id = prefs.getString(createKEY(total_ID), null);
        if (id != null) {
            arrID.add(id);
        }
        total_ID -=1;
    }
    adapterID = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, arrID);
    autoID.setAdapter(adapterID);
}
private void addCache(String data) {
    for (int i = 0; i < arrID.size(); i++) {
        String str = arrID.get(i);
        if (str.trim().equalsIgnoreCase(data.trim())) {
            return;
        }
    }

    arrID.add(data);
    adapterID = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, arrID);
    autoID.setAdapter(adapterID);

    putIntSharedPreferences("COUNT_ID", arrID.size());
    putStringSharedPreferences(createKEY(arrID.size()), data);
}

private String createKEY(int total) {
    String key = "ID"+total;
    return key;
}
private void putIntSharedPreferences(String key, int value) {
    SharedPreferences preferences = getSharedPreferences("LOGINS",
            MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt(key, value);
    editor.commit();
}
private void putStringSharedPreferences(String key, String values) {
    SharedPreferences preferences = getSharedPreferences("LOGINS",
            MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString(key, values);
    editor.commit();
}

private String getSharedPreferences(String key) {
    SharedPreferences preferences = getSharedPreferences("LOGINS",
            MODE_PRIVATE);
    return preferences.getString(key, "VALUE 1");
}

当onclick到Submit按钮时,我使用SharedPreferences来保存ID。