package com.yokmama.learn10.chapter06.lesson27; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.support.v7.widget.GridLayout; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView mTvPreview; private Calculator mCalculator = new Calculator(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lesson27_activity_main); getSupportActionBar().hide(); //インスタンスを取得 mTvPreview = (TextView) findViewById(R.id.preview); findViewById(R.id.button0).setOnClickListener(this); findViewById(R.id.button1).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); findViewById(R.id.button3).setOnClickListener(this); findViewById(R.id.button4).setOnClickListener(this); findViewById(R.id.button5).setOnClickListener(this); findViewById(R.id.button6).setOnClickListener(this); findViewById(R.id.button7).setOnClickListener(this); findViewById(R.id.button8).setOnClickListener(this); findViewById(R.id.button9).setOnClickListener(this); findViewById(R.id.dot).setOnClickListener(this); findViewById(R.id.equal).setOnClickListener(this); findViewById(R.id.sum).setOnClickListener(this); findViewById(R.id.difference).setOnClickListener(this); findViewById(R.id.product).setOnClickListener(this); findViewById(R.id.quotient).setOnClickListener(this); findViewById(R.id.clear).setOnClickListener(this); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); // GridLayout内のアイテムをレイアウトサイズに合わせてストレッチ final GridLayout gl = (GridLayout) findViewById(R.id.calcFrame); int childWidth = gl.getWidth() / gl.getColumnCount(); int childHeight = gl.getHeight() / gl.getRowCount(); for (int i = 0; i < gl.getChildCount(); i++) { gl.getChildAt(i).setMinimumWidth(childWidth); gl.getChildAt(i).setMinimumHeight(childHeight); } } @Override public void onClick(View v) { if (v.getId() == R.id.clear) { //計算をリセット mCalculator.reset(); mTvPreview.setText("0"); } else { //入力した値を元に計算 String input = ((Button) v).getText().toString(); String dispText = mCalculator.input(input); //計算結果をTextViewに表示 if (!TextUtils.isEmpty(dispText)) { mTvPreview.setText(dispText); } } } }