android:为微调器视图添加唯一标签或 ID
android: add unique tag or id for spinner view
我在我的应用程序中创建了 4 个微调器视图,当我更改其中一个上的选定项时,我想知道选择并更改了哪个微调器。我使用此代码:
for(int i=0;i<4;i++){
final Spinner spinner= new Spinner(context);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});}
如何为每个微调器设置唯一标签并 return 在 ItemSelectedListner 上设置此标签?
谢谢
在您的 onItemSelected() 中使用 parent.getId() 来获取其项目被选中的微调器的 ID。工作代码:
for(int i=0;i<4;i++){
final Spinner spinner= new Spinner(context);
spinner.setId(i);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
switch(parent.getId()){
case "0": //your code
case "1": //your code
...
}
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
这样做:
for(int i=0;i<4;i++){
final Spinner spinner= new Spinner(context);
spinner.setTag(i);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (parent.getTag().equals("0")){
//your code
}
else if (parent.getTag().equals("1")){
//your code
}
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});}
我在我的应用程序中创建了 4 个微调器视图,当我更改其中一个上的选定项时,我想知道选择并更改了哪个微调器。我使用此代码:
for(int i=0;i<4;i++){
final Spinner spinner= new Spinner(context);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});}
如何为每个微调器设置唯一标签并 return 在 ItemSelectedListner 上设置此标签? 谢谢
在您的 onItemSelected() 中使用 parent.getId() 来获取其项目被选中的微调器的 ID。工作代码:
for(int i=0;i<4;i++){
final Spinner spinner= new Spinner(context);
spinner.setId(i);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
switch(parent.getId()){
case "0": //your code
case "1": //your code
...
}
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
这样做:
for(int i=0;i<4;i++){
final Spinner spinner= new Spinner(context);
spinner.setTag(i);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (parent.getTag().equals("0")){
//your code
}
else if (parent.getTag().equals("1")){
//your code
}
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});}