朋友们,我正在使用AutoCompleteTextView。建议:
String[] recipes={ "Fish", "Chicken", "Mutton"};
我该怎么做:
当我从下拉列表中选择其中一个项目时,它会转到另一个事件吗?
例如,我输入 Fi ,它会从下拉列表中显示 Fish ,然后我选择Fish,它将转到另一个活动
package net.learn2develop.Activities;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.view.View;
import android.content.Intent;
public class AutoCompleteTextActivity extends Activity {
String[] recipes ={
"Nasi Lemak With Ikan Bilis",
"Steamed Cod Fish"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, recipes);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtRecipes);
textView.setThreshold(3);
textView.setAdapter(adapter);
textView.setOnItemSelectedListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> av, View view, int index, long id){
Intent i=new Intent(this,Activity2.class);
i.putExtra("item",recipes[index]);
StartActivity(i);
}
});
}
}
答案 0 :(得分:0)
您可以使用实现TextWatcher的类并覆盖以下方法:
@Override
public void afterTextChanged(final Editable editable) {
// check if entered text is "fish" and if yes then start the new activity.
}
@Override
public void beforeTextChanged(final CharSequence string,
final int start, final int count, final int after) {
}
@Override
public void onTextChanged(final CharSequence string, final int start,
final int before, final int count) {
}
}
答案 1 :(得分:0)
receipesBelow Snippet会帮助你。
autoCompleteTextView.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> av, View view,
int index, long id)
{
//index will give you item which you selected
Now start another activity here
Intent i=new Intent(context,SecondActivity.class);
i.putExtra("item",recipes[index]);
StartActivity(i);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
})
<强> SecondActivity.java 强>
您可以检索该值是onCreate,如下所示
String selectedItem=getIntent().getStringExtra("item");
答案 2 :(得分:0)
您应该使用setOnItemClickListener
代替setOnItemSelectedListener
。