android中的微调框

时间:2018-09-05 12:00:19

标签: java android spinner

我正在尝试创建多个微调器onItemSelected函数,但是它在父视图中给了我错误。

public void onItemSelected(AdapterView<?> **parent**, **View** view,
    int pos, long id) {
        parent.getItemAtPosition(pos);

        TextView  tvReadValue = (TextView) findViewById(R.id.tvReadValue);
        switch (parent.getId())
        {
            case R.id.spinDimension:

                tvReadValue.setText("spinDimension" + pos);

                break;

            case R.id.speedDimension:

                tvReadValue.setText("speedDimension" + pos);
                break;
        }

**是我的错误

更新。我的问题是我有3个微调器,并且都对它们实施了onselect。当我在第一个微调器中选择一个值时。它也可以在其他微调器的选定方法上运行...

2 个答案:

答案 0 :(得分:0)

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
 Log.d("form","onitemselected");
 switch (parent.getId()) {
 case R.id.spinDimension:
 tvReadValue.setText("spinDimension" + position);
 Log.d("form","regionid:" + r.id);
 break;
 case R.id.speedDimension:
 tvReadValue.setText("speedDimension" + position);
 Log.d("form","state id:");
 break;
}

答案 1 :(得分:0)

尝试

Spinner firstSpinner = (Spinner) findViewById(R.id.spinner1);    
spinner.setOnItemSelectedListener(this);

...

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    Toast.makeText(parent.getContext(), 
        "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
        Toast.LENGTH_SHORT).show();
}

也可以直接将其设置在所需位置。在设置好适配器后调用微调器。

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
           @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        switch (position) {
            case 0:
                Toast.makeText(parent.getContext(), "Spinner item 1!", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(parent.getContext(), "Spinner item 2!", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(parent.getContext(), "Spinner item 3!", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

       // sometimes you need nothing here
    }
});