gpt4 book ai didi

java - 如何使自动完成预测可点击?

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:46 25 4
gpt4 key购买 nike

当我输入某些内容时,我会得到自动完成预测。就像当我输入“Joh”时,我得到“John F. Kennedy”。现在,如果用户单击“John F. Kennedy”,他就会进入一项 Activity ,向他提供有关 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

最佳答案

使用此代码。

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();

}
});

}

关于java - 如何使自动完成预测可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22877325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com