微调OnItemSelectedListener问题 [英] Spinner OnItemSelectedListener Issue
问题描述
我有微调控制问题。我想动态设置微调的项目。起初,我有一个项目的微调。
I have problem with spinner control. I am trying to set spinner items dynamically. Initially I have one item in spinner.
当我尝试注册spinner.setOnItemSelect监听器,它会立即调用它onItemSelected方法。不过,我并不想只要我的活动开始调用此方法。
When I try to register the spinner.setOnItemSelect Listener, it immediately call onItemSelected method of it. However I don't want to call this method as soon as my activity get started.
因此,对于这个我把下面的条件。
So for this I put a following condition.
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
Spinner spinner;
String[] str_arr = {"aaaaaaaa"};
private int mSpinnerCount=0;
private int mSpinnerInitializedCount=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str_arr));
spinner.setOnItemSelectedListener(this);
mSpinnerCount=1;
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (mSpinnerInitializedCount < mSpinnerCount) {
mSpinnerInitializedCount++;
}
else {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
但是,当我尝试在微调选择一个项目它给出的logcat以下警告,
But when I try to select an item on spinner it gives following warning in logcat,
13 09-03:02:02.528:W / InputManagerService(59):窗口已经集中,忽视聚焦增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@450fafb8
09-03 13:02:02.528: W/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@450fafb8
我得到的想法,之前,除非微调不会改变这个方法的项目将不会被调用。
I get the idea that until and unless Item of spinner won't change this method won't be called.
不过,我有微调的一个值,所以如何获得焦点,任何想法?
But I have one value in spinner, so how to get the focus, any idea?
推荐答案
试试这样
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (position != 0) {
//put your actions here
}
else {
// nothing here or toast
}
}
这篇关于微调OnItemSelectedListener问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!