我有一个具有微调器的活动,这是处理该微调器内的on项选择的方法。
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
// Do stuff based on selection
}
}
此外,我有一个按钮,代码看起来像这样
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// This doesnt work
onItemSelected(MyProfileActivity.this, v, 0, 2);
}
});
}
基本上我要做的就是通过按钮模拟在微调器中选择的项目。这甚至可能吗?
答案 0 :(得分:4)
不要在按钮上调用onItemSelected
方法,而应使用View.performItemClick
方法点击按钮上的微调器项目单击它:
int item_postion=0;// item which you want to click
your_spinner.setSelection(item_postion, true);
View item_view = (View)your_spinner.getChildAt(item_postion);
long item_id = your_spinner.getAdapter().getItemId(item_postion);
your_spinner.performItemClick(item_view, 0, item_id);
答案 1 :(得分:1)
试试这个..
btnChangeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
spinner.setSelection(index); // which you what set the spinner position
String item = spinner.getSelectedItem().toString().trim(); //which you need to get the selected item from the spinner
System.out.println("Selected item : "+item):
}
});
答案 2 :(得分:1)
试试这段代码:
spin.performItemClick(view, position, id);