關於spinner運用
程式碼參考如下
package COM.TQC.GDD01; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.SpinnerAdapter; import android.widget.TextView; import android.widget.Toast; public class GDD01 extends Activity { protected TextView MyOutcome; protected int volt; protected int inputv = 110; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spinner(); button(); } private void spinner() { // TODO Auto-generated method stub Spinner Sp=(Spinner)findViewById(R.id.input_v); Sp.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub volt=arg0.getSelectedItemPosition(); if(volt ==0) { inputv=110; } else { inputv=220; } } @Override public void onNothingSelected(AdapterView arg0) { // TODO Auto-generated method stub } }); } private void button() { // TODO Auto-generated method stub Button btn=(Button)findViewById(R.id.submit); btn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub EditText fill_a=(EditText)findViewById(R.id.input_a); EditText fill_hr=(EditText)findViewById(R.id.input_hr); EditText fill_m=(EditText)findViewById(R.id.input_m); double inputa=Double.parseDouble(fill_a.getText().toString()); double inputhr=Double.parseDouble(fill_hr.getText().toString()); double inputm=Double.parseDouble(fill_m.getText().toString()); double total_w= inputv * inputa; int total_m=(int) Math.round((total_w/1000)*(inputhr*30)*inputm); MyOutcome=(TextView)findViewById(R.id.respond); MyOutcome.setText("$"+total_m); } }); } }