在微调器中选择任何类型时,微调器有一个乐队类型的数组列表,它将显示一个吐司,当点击微调器时,toast没有显示,代码没有显示任何错误?任何想法为什么?
String spinnerSelection = null;
Spinner spinner = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_item, spinnerContent);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onItemSelected(AdapterView<?>parent, View v, int position, long id) {
{
Toast.makeText( getBaseContext(),"Hell Yeah", Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
答案 0 :(得分:0)
不要忘记在微调器中添加点击侦听器
spinner.setOnItemClickListener(this);
答案 1 :(得分:0)
您正在按钮上添加setOnClickListener()。将setOnItemClickListener()添加到您的微调器。
答案 2 :(得分:0)
您需要为@Override
和onItemSelected(...)
函数添加onNothingSelected(...)
。
确保在微调器上设置一个侦听器:
spinner.setOnItemClickListener(this);
修改
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
MediaPlayer myMusic;
int paused;
public void play(View view) {
if (myMusic == null) {
myMusic = MediaPlayer.create( this, R.raw.fast_and_cold );
myMusic.start();
}
}
public void stop(View view) {
myMusic.reset();
myMusic = null;
}
String spinnerSelection = null;
Spinner spinner = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
final List<String> spinnerContent = new ArrayList<>();
spinnerContent.add("Black Metal");
spinnerContent.add("Death Metal");
spinnerContent.add("Thrash Metal");
spinnerContent.add("Heavy Metal");
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_item, spinnerContent);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this); // add this line
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
}
@Override // add this line
public void onItemSelected(AdapterView<?>parent, View v, int position, long id) {
Toast.makeText( getBaseContext(),"Show your Devil Horns !!", Toast.LENGTH_SHORT).show();
}
@Override // add this line
public void onNothingSelected(AdapterView<?> arg0) {
}
}