public class service extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
Spinner spinner1 = findViewById(R.id.spinner1); // Spinner code
spinner1.setOnItemSelectedListener(this);
Spinner spinner2 = findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
R.array.serviceD,
android.R.layout.simple_spinner_item
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(this, adapterView.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
如何在同一页面中添加另一个微调器?
最佳答案
在onNothingSelected
和onItemSelected
方法中,您可以像下面那样检查id-
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
int id = adapterView.getId();
switch (id)
{
case R.id.spinner1:
Log.d("Selected_Value","Spinner 1 : "+adapterView.getItemAtPosition(i).toString());
break;
case R.id.spinner2:
Log.d("Selected_Value","Spinner 2 : "+adapterView.getItemAtPosition(i).toString());
break;
}
}