gpt4 book ai didi

android - 如何在autocompleteTextview中添加onItemSelectedListener

转载 作者:行者123 更新时间:2023-11-30 02:51:59 25 4
gpt4 key购买 nike

我正在尝试添加一个自动完成功能并在该按钮点击事件中设置一个 itemItemSelectListener

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,new ArrayList<String>());

//This is working fine------------------------------------------
editPhoneNum.setThreshold(1);
editPhoneNum.setAdapter(adapter);
editPhoneNum.setOnItemSelectedListener(this);
editPhoneNum.setOnItemClickListener(this);
//---------------------------------------------------------------

AddCont_btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Counter++;
AutoCompleteTextView = new AutoCompleteTextView(sentsms.this);
AutoCompleteTextView.setThreshold(1);
AutoCompleteTextView.setOnItemSelectedListener(sentsms.this); //<--This doesn't work
AutoCompleteTextView.setAdapter(adapter);
rel_lay.addView(AutoCompleteTextView);

}
});

如何解决这个问题谢谢大家..!!!!

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
Log.d("AutocompleteContacts"," Name:" + arg0.getItemAtPosition(arg2) + "\n Number:"+ toNumberValue);
}

XMLrel_lay 是一个线性布局

 <LinearLayout
android:id="@+id/rel_lay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >

<AutoCompleteTextView
android:id="@+id/addCont1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter Frnds Name"
android:text="" >
</AutoCompleteTextView>
</LinearLayout>
</LinearLayout>

当 AutoCompleteTextView 选择项目时不调用此方法

最佳答案

从 xml 文件的 rel_lay 中删除自动完成

AddCont_btn.setOnClickListener(new OnClickListener() 
{
@Override
public void onClick(View v)
{
Counter++;
AutoCompleteTextView = new AutoCompleteTextView(sentsms.this);
AutoCompleteTextView.setThreshold(1);
//make unique id for each autocomplete then only you can refer it individually
AutoCompleteTextView.setId(your_unique_id);
AutoCompleteTextView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
AutoCompleteTextView.setOnItemSelectedListener(sentsms.this);
AutoCompleteTextView.setAdapter(adapter);
rel_lay.addView(AutoCompleteTextView);

}
});

并在

中执行必要的步骤
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//TODO Auto-generated method stub
//do here Whatever is your objective on item click

}

关于android - 如何在autocompleteTextview中添加onItemSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23988101/

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