如何在TextView中显示两个微调器(存在于一个活动中)的值:Android

时间:2015-08-02 09:53:17

标签: android android-studio textview spinner

我无法在TextView中显示所选微调器的值。 没有显示任何微调器值。 有没有办法显示两个微调器的值?

这是代码

public class Calculator extends Activity implements OnItemSelectedListener {

    Spinner temp1,temp2;
    TextView t1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        t1=(TextView)findViewById(R.id.t1);

        temp1=(Spinner)findViewById(R.id.temp);
        ArrayAdapter<CharSequence>   adapter=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    temp1.setAdapter(adapter);

       temp2=(Spinner)findViewById(R.id.temp2);
       ArrayAdapter<CharSequence> adapter2=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
      adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    temp2.setAdapter(adapter2);
    }

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

        Spinner temp1=(Spinner)parent;
        Spinner temp2=(Spinner)parent;
        if (temp1.getId()==R.id.temp) {
            String item = parent.getItemAtPosition(position).toString();
            t1.setText(item);
        }
        if (temp2.getId()==R.id.temp2) {
            String item = parent.getItemAtPosition(position).toString();
            t1.setText(item);
        }

    }

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

    }
}

1 个答案:

答案 0 :(得分:1)

您没有向您的微调器添加监听器。

使用:

temp1.setOnItemSelectedListener(this);
temp2.setOnItemSelectedListener(this);