1 package com.example.yungchen.ex03; 2 3 import android.app.Activity; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.MotionEvent; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.view.inputmethod.InputMethodManager; 10 import android.widget.AdapterView; 11 import android.widget.ArrayAdapter; 12 import android.widget.Button; 13 import android.widget.EditText; 14 import android.widget.Spinner; 15 import android.widget.TextView; 16 17 public class MainActivity extends AppCompatActivity { 18 protected Spinner spinOP1; 19 protected TextView tvResult1; 20 protected Button btnRun1; 21 protected EditText etVal1, etVal2; 22 protected int opType = 0; // 0 => +, 1 => -, 2 => *, 3 => / 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 setupUI(findViewById(R.id.parent)); 29 30 tvResult1 = (TextView) findViewById(R.id.tvResult); 31 32 spinOP1 = (Spinner) findViewById(R.id.spinOP); 33 ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.support_simple_spinner_dropdown_item); 34 adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 35 spinOP1.setAdapter(adapter1); 36 spinOP1.setOnItemSelectedListener(new SpinnerActivity()); 37 38 etVal1 = (EditText) findViewById(R.id.etV1); 39 etVal2 = (EditText) findViewById(R.id.etV2); 40 41 btnRun1 = (Button) findViewById(R.id.runBtn); 42 btnRun1.setOnClickListener(new View.OnClickListener() { 43 @Override 44 public void onClick(View v) { 45 float tmpVal = 0.0f, 46 v1 = Float.parseFloat(etVal1.getText().toString()), 47 v2 = Float.parseFloat(etVal2.getText().toString()); 48 switch (opType){ 49 case 0: 50 tmpVal = v1 + v2; 51 break; 52 case 1: 53 tmpVal = v1 - v2; 54 break; 55 case 2: 56 tmpVal = v1 * v2; 57 break; 58 case 3: 59 tmpVal = v1 / v2; 60 break; 61 } 62 tvResult1.setText(Float.toString(tmpVal)); 63 } 64 }); 65 } 66 67 public static void hideSoftKeyboard(Activity activity) { 68 InputMethodManager inputMethodManager = 69 (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 70 inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); 71 } 72 73 public void setupUI(View view) { 74 //Set up touch listener for non-text box views to hide keyboard. 75 if(!(view instanceof EditText)) { 76 view.setOnTouchListener(new View.OnTouchListener() { 77 public boolean onTouch(View v, MotionEvent event) { 78 hideSoftKeyboard(MainActivity.this); 79 return false; 80 } 81 }); 82 } 83 84 //If a layout container, iterate over children and seed recursion. 85 if (view instanceof ViewGroup) { 86 for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 87 View innerView = ((ViewGroup) view).getChildAt(i); 88 setupUI(innerView); 89 } 90 } 91 } 92 93 class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener { 94 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 95 // An item was selected. You can retrieve the selected item using 96 // parent.getItemAtPosition(pos) 97 opType = pos; 98 } 99 100 public void onNothingSelected(AdapterView<?> parent) { 101 // Another interface callback 102 } 103 } 104 }