さて午後。
#「onActivityResult()」は後で調べよう。
★ページ遷移+計算
出来たには出来たけど、普通すぎる、、、
[MainActivity.java]
あれ、ボタンを1まとめに出来るんだ・・・
#「onActivityResult()」は後で調べよう。
★ページ遷移+計算
出来たには出来たけど、普通すぎる、、、
[MainActivity.java]
public class MainActivity extends Activity { EditText et1; EditText et2; Button btn1; Button btn2; Button btn3; Button btn4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findView(); addEvent(); } private void findView() { et1 = (EditText)findViewById(R.id.editText1); et2 = (EditText)findViewById(R.id.editText2); btn1 = (Button)findViewById(R.id.button1); btn2 = (Button)findViewById(R.id.button2); btn3 = (Button)findViewById(R.id.button3); btn4 = (Button)findViewById(R.id.button4); } private void addEvent() { btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { a(1); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { a(2); } }); btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { a(3); } }); btn4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { a(4); } }); } public void a(int mathMarkNum){ //数値を取得 int num1 = Integer.parseInt(et1.getText().toString()); int num2 = Integer.parseInt(et2.getText().toString()); int sum = 0; switch(mathMarkNum){ case 1: sum = num1+num2; break; case 2: sum = num1-num2; break; case 3: sum = num1*num2; break; case 4: sum = num1/num2; break; } Intent i = new Intent(MainActivity.this, ResultActivity.class); i.putExtra("NUM", sum+""); startActivity(i); } }[ResultActivity.java]
public class ResultActivity extends Activity { TextView tv; Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_result); findView(); addEvent(); } private void findView() { tv = (TextView)findViewById(R.id.textView1); bt = (Button)findViewById(R.id.button1); Intent i= this.getIntent(); String num = i.getStringExtra("NUM"); tv.setText(num); } private void addEvent() { bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
あれ、ボタンを1まとめに出来るんだ・・・
public class MainActivity extends Activity implements OnClickListener{ EditText et1; EditText et2; Button btAdd; Button btSub; Button btMul; Button btDiv; int ans; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findView(); } private void findView() { et1=(EditText)findViewById(R.id.editText1); et2=(EditText)findViewById(R.id.editText2); btAdd=(Button)findViewById(R.id.button1); btAdd.setOnClickListener(this); btSub=(Button)findViewById(R.id.button2); btSub.setOnClickListener(this); btMul=(Button)findViewById(R.id.button3); btMul.setOnClickListener(this); btDiv=(Button)findViewById(R.id.button4); btDiv.setOnClickListener(this); } @Override public void onClick(View v) { int a=Integer.parseInt(et1.getText().toString()); int b=Integer.parseInt(et2.getText().toString()); switch(v.getId()){ case R.id.button1: ans=a+b; break; case R.id.button2: ans=a-b; break; case R.id.button3: ans=a*b; break; case R.id.button4: ans=a/b; break; } Intent i=new Intent(MainActivity.this,ResultActivity.class); i.putExtra("ans", ans); startActivity(i); } }
コメント