gpt4 book ai didi

java - Android Spinner OnItemSelected 不起作用

转载 作者:行者123 更新时间:2023-12-01 14:18:28 25 4
gpt4 key购买 nike

我有三个不同的 Spinner 对象(其中两个具有自定义布局,一个 - spMode - 是标准布局)。

当按下OkayButton 时,应显示一个带有3 个数字的Toast:Mode、Color1 和Color2 的SpinnerItemNumber。但每次按下按钮时Mode都是0。其他人工作正常。即使我在应用程序中选择了项目 2 或 3,仅模式也显示项目 0 被选中。

这是相关代码;有什么想法吗?

public class NewTimer extends Activity implements OnItemSelectedListener,OnClickListener
{
Button btnOkay;
EditText etTime;
int mode, minutes, color1, color2;
String[] strings = {"","",
"", "", "", ""};

int arr_images[] = { R.drawable.yellow,
R.drawable.orange, R.drawable.red,
R.drawable.blue, R.drawable.green, R.drawable.white};

public void onClick(View v)
{
if (v == btnOkay)
{
Toast.makeText(v.getContext(), Integer.toString(mode) + Integer.toString(color1) + Integer.toString(color2), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newtimer);
Spinner spColor1 = (Spinner)findViewById(R.id.spColor1);
spColor1.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
spColor1.setOnItemSelectedListener(this);
Spinner spColor2 = (Spinner)findViewById(R.id.spColor2);
spColor2.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
spColor2.setOnItemSelectedListener(this);
etTime = (EditText) findViewById(R.id.etTime);
btnOkay = (Button) findViewById(R.id.btnOkay);
btnOkay.setOnClickListener(this);

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

public void onItemSelected(AdapterView<?> parent,
View v, int pos, long id)
{
switch (parent.getId())
{
case R.id.spMode:
mode = pos;
break;

case R.id.spColor1:
color1 = pos;
break;

case R.id.spColor2:
color2 = pos;
break;
}
}

public void onNothingSelected(AdapterView<?> parent)
{

}



public class MyAdapter extends ArrayAdapter
{
public MyAdapter(Context context, int textViewResourceId,
String[] objects)
{
super(context, textViewResourceId, objects);
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater=getLayoutInflater();View row=inflater.inflate(R.layout.row, parent, false);
TextView label=(TextView)row.findViewById(R.id.color);
label.setText(strings[position]);
ImageView icon=(ImageView)row.findViewById(R.id.image);
icon.setImageResource(arr_images[position]);
return row;
}
}
}

最佳答案

您从未为该微调器设置setOnItemSelectedListener:

@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newtimer);
Spinner spColor1 = (Spinner)findViewById(R.id.spColor1);
spColor1.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
spColor1.setOnItemSelectedListener(this);
Spinner spColor2 = (Spinner)findViewById(R.id.spColor2);
spColor2.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
spColor2.setOnItemSelectedListener(this);
etTime = (EditText) findViewById(R.id.etTime);
btnOkay = (Button) findViewById(R.id.btnOkay);
btnOkay.setOnClickListener(this);

Spinner spMode = (Spinner) findViewById(R.id.spMode);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spMode_items, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spMode.setAdapter(adapter);
spMode.setOnItemSelectedListener(this); }

public void onItemSelected(AdapterView<?> parent,
View v, int pos, long id) {
switch (parent.getId())
{
case R.id.spMode:
mode = pos;
break;

case R.id.spColor1:
color1 = pos;
break;

case R.id.spColor2:
color2 = pos;
break;
} }

关于java - Android Spinner OnItemSelected 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17883581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com