如何使自动完成预测可单击?

时间:2014-04-05 06:17:28

标签: java android autocomplete clickable

当我输入内容时,我会得到一个自动完成预测。就像我进入" Joh"时,我得到了约翰肯尼迪"。现在,如果用户点击" John F. Kennedy",则会将他带入一个活动,向他提供有关John F. Kennedy的信息。我该怎么做才能得到这个?如何使自动填充预测可以点击?

package com.mavenmaverick.autocomplete_test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;


public class MainActivity extends Activity {

String[] presidents= { "John F. Kennedy",
                    "Lyndon B. Johnson",
                    "Richard Nixon",
                    "Gerald Ford",
                    "Jimmy Carter",
                    "Ronald Reagan",
                    "George H. W. Bush",
                    "Bill Clinton",
                    "George W. Bush",
                    "Barack Obama"
                    };

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

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, presidents);

    textView.setThreshold(3);
    textView.setAdapter(adapter);

}

}

Autocomplete_Test

2 个答案:

答案 0 :(得分:0)

你可以尝试下面的代码,

autoCompleteTextview.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff
    }
})

当用户使用setOnItemSelectedListener实际选择下拉列表中的项目时,您还有一个选项来设置点击侦听器。

autoCompleteTextview.setOnSelectedListener(new OnItemClickListener() {
    @Override
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff
    }
});

修改

if(position == 1){
Intent i = new Intent (MainActivity.this, johncanedy.class);
startActivity(i);
}else if (position == 2){
Intent i = new Intent (MainActivity.this, lyndon.class);
startActivity(i);
} 

依旧......

答案 1 :(得分:0)

使用此代码。

String[] presidents= { "John F. Kennedy",
                    "Lyndon B. Johnson",
                    "Richard Nixon",
                    "Gerald Ford",
                    "Jimmy Carter",
                    "Ronald Reagan",
                    "George H. W. Bush",
                    "Bill Clinton",
                    "George W. Bush",
                    "Barack Obama"
                    };
private AutoCompleteTextView myAutoComplete;

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

        myAutoComplete = (AutoCompleteTextView) findViewById(R.id.myautocomplete);
        myAutoComplete.addTextChangedListener(this);
        myAutoComplete.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, presidents));



myAutoComplete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getBaseContext(), InforActivity.class);
                    intent.putExtra("president_name", obj.toString());
                    startActivity(intent);
                    finish();

            }
        });

}