如何使用历史记录制作AutoCompleteTextView

时间:2017-02-06 20:54:38

标签: android android-edittext autocompletetextview

好吧,我已经有了一个AutoCompleteTextView,如果我把它写在字符串上,它已经显示了一些建议。

但我喜欢他记得用户写的内容。 请参阅以下示例:

Google Search History

到目前为止,这是我的代码:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, equipas);
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.editText1);
    AutoCompleteTextView textView2 = (AutoCompleteTextView) findViewById(R.id.editText2);
    textView.setAdapter(adapter);
    textView2.setAdapter(adapter);

}

private static final String[] equipas = new String[] {
        //Insert what the user writes!
};

修改

以下是activity.xml中的AutoCompleteTextView:

AutoCompleteTextView Location

修改

这是完整的代码:

package com.example.caca_a_frase;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import static com.example.caca_a_frase.Opcoes.som;
import static com.example.caca_a_frase.Opcoes.vibrar; 
import static com.example.caca_a_frase.SplashScreen.player;
import static com.example.caca_a_frase.Menus.shouldPlay;

public class Nomes extends Activity {

private static Button comecar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_nomes);

    shouldPlay = false;

    onClickButtonListener();

    findViewById(R.id.fullscreen_content_controls);
    findViewById(R.id.fullscreen_content);

    //Atribuir o tipo de letra:
    Typeface peint = Typeface.createFromAsset(getAssets(), "peinture_freiche.ttf");
    TextView equipum = (TextView)findViewById(R.id.editText1);
    equipum.setTypeface(peint);
    TextView equipdois = (TextView)findViewById(R.id.editText2);
    equipdois.setTypeface(peint);
    //Acaba aqui!

    //Para ver a lista de nomes que já usaram:
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, equipas);
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.editText1);
    AutoCompleteTextView textView2 = (AutoCompleteTextView) findViewById(R.id.editText2);
    textView.setAdapter(adapter);
    textView2.setAdapter(adapter);
    //Acaba aqui!

}

private static final String[] equipas = new String[] {
        //Insert what the user writes!
};

@Override
protected void onPause() {
        super.onPause();
        if (shouldPlay == false){ //A música é parada se o "shouldPlay" for falso!
            player.pause();
        }
    }
@Override
protected void onResume() {
        super.onResume();
        if (shouldPlay == false){  //A música volta a ser tocada de onde estava se o "shouldPlay" for falso!
            player.start();
        }
        else {
            shouldPlay = false;
        }
    }

public void onClickButtonListener() {
    comecar = (Button) findViewById(R.id.btComecar);
    final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.click);
    comecar.setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (som == true) {
                        mp2.start();
                       } if (vibrar == true) {
                        Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                        vib.vibrate(100);
                       }
                    //Para ler o texto escrito:

                    EditText equipaum = (EditText) findViewById(R.id.editText1);
                    EditText equipadois = (EditText) findViewById(R.id.editText2);

                    //Acaba aqui!
                    Intent intent = new Intent("com.example.caca_a_frase.Game");
                    //Para guardar o texto com uma chave para outra atividade:

                    intent.putExtra("equipaum", equipaum.getText().toString());
                    intent.putExtra("equipadois", equipadois.getText().toString());

                    //Acaba aqui!
                    shouldPlay = true;
                    startActivity(intent);
                }
            });

}
@Override
public void onBackPressed() {
    Intent voltar = new Intent("com.example.caca_a_frase.Menus");
    shouldPlay = true;
    startActivity(voltar);
    }
}

1 个答案:

答案 0 :(得分:0)

equipas需要成为Arraylist

如果您致电adapter.add("value"),那么&#34;价值&#34;将在您的建议/历史中。