我想在textview中显示微调器内容。我可以在textview中显示微调器内容,但只显示第一个元素,如果ai更改项目,textview不会更改
Spinner sp = (Spinner) findViewById(R.id.cbTipoInspeccion);
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.tipoPrioridad, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
tvMostrar=(TextView)this.findViewById(R.id.tvSpinnerContent);
String Text = sp.getSelectedItem().toString();
tvMostrar.setText(Text.toString());
答案 0 :(得分:2)
您需要在微调器上设置OnItemSelectedListener:
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//update your TextView
tvMostrar.setText(sp.getSelectedItem().toString());
}
onNothingSelected(AdapterView<?> parent) {
//
}
})
查看Spinners和OnItemSelectedListener上的文档。