我试图弄清楚如何使用用户选择的 switch-case base 打印一个值。我可以轻松打印用户在微调器中选择的内容,但我需要相应的值。我希望我只是在我的代码中遗漏了一些小东西。
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class Test extends Activity implements OnClickListener {
EditText input1;
Spinner answer;
Object value;
int yourInt = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
input1 = (EditText) findViewById (R.id.editText1);
answer = (Spinner) findViewById (R.id.spinner1);
((Button) findViewById (R.id.button1)).setOnClickListener(this);
answer.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
value = parent.getItemAtPosition(position);
switch (position) {
case 0:
yourInt = 1;
break;
case 1:
yourInt = 2;
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void onClick (View v) {
input1.setText(yourInt);
}
}