0

我尝试在按下按钮时动态创建微调器。

这就是我所做的:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.leave_balance);

spinner = new Spinner(this);
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, list);
    spinner.setAdapter(spinnerArrayAdapter);

Button  b = (Button)findviewbyid(R.id.button1);
b.setonclicklistener(new Onclicklistener){
   public void onClick(View v) {
            spinner.performClick();


        }
 spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.maketext(context,"selectedspinner",Toast.LENGTH_SHORT).show();

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
}
}

我的问题是我无法得到那个吐司,即微调器 onitemselectedlistner 没有触发。

任何建议将不胜感激..谢谢

4

3 回答 3

3

Spinner 需要添加一个父级才能调用onItemSelected

  1. 在 XML 中添加旋转器并将宽度和高度设置为 0dp(可以设置可见性不可见不消失)。

  2. 使用findViewById.

于 2012-06-27T10:36:53.893 回答
2

spinner.performClick() 调用 onclicklistener 而不是 onItemclicklistener。

使用 spinner.performitemclick(.....);

于 2012-06-27T10:36:44.207 回答
0

试试这个。

onCreate()
{

spinnerLocation = (Spinner)findViewById(R.id.spinnerSettingLocation);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.location_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerLocation.setAdapter(adapter);
    spinnerLocation.setOnItemSelectedListener(new MyOnLocationSpinnerItemSelectedListener());

}



public class MyOnLocationSpinnerItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
        Toast.maketext(context,"selectedspinner" + pos,Toast.LENGTH_SHORT).show();

    }




    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}
于 2012-06-27T10:37:48.710 回答