我是 android 新手,任何人都可以帮我解决这个简单的问题,但我无法识别这个执行过程。我需要通过 Spinner 动态获取结果,但这里 Spinner 仅在运行时执行(不点击 Spinner)对不起问这个问题我无法解决这个问题......:(
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.loanpayment);
try {
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
et3 = (EditText) findViewById(R.id.et3);
ans = (TextView) findViewById(R.id.editText1);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
s = (Spinner) findViewById(R.id.spin);
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
ArrayAdapter<String> a = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, values);
a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(a);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String item = parent.getItemAtPosition(position).toString();
if(item.equals("Yearly")) {
Pattern p = Pattern.compile("([0-9 .]*)");
if (et1.getText().toString().trim().length() > 0 && et2.getText().toString().trim().length() > 0 && et3.getText().toString().trim().length() > 0) {
Matcher m = p.matcher(et1.getText().toString().trim());
Matcher m1 = p.matcher(et2.getText().toString().trim());
Matcher m2 = p.matcher(et3.getText().toString().trim());
if (m.matches() && m1.matches() && m2.matches()) {
amount = Float.valueOf(et1.getText().toString());
interest = Float.valueOf(et2.getText().toString());
year = Float.valueOf(et3.getText().toString());
s1 = interest/100;
s2 = year;
ans1 = s1 * amount;
ans2 = (float) (1-Math.pow((1+s1), -s2));
tot = (float) Math.round(ans1/ans2);
String an = Float.toString(tot);
ans.setText("Amount to Pay=" +an);
}
else {
Toast.makeText(getApplicationContext(),
"Incorrect value", Toast.LENGTH_SHORT)
.show();
}