Created
June 10, 2020 01:57
-
-
Save godjooyoung/ed0c4785bddc92dcb98890048732ee21 to your computer and use it in GitHub Desktop.
0610 이벤트 리스너 걸기
SETTEXT를 통해 결과를 뿌려줄수도 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.calc; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import androidx.appcompat.app.AppCompatActivity; | |
public class MainActivity extends AppCompatActivity { | |
Button addbtn; | |
Button btn2; | |
Button btn3; | |
Button btn4; | |
EditText num1; | |
EditText num2; | |
//전역변수로 쓰기위해 밖에서 먼저 정의를 해준것입니다. | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
addbtn =findViewById(R.id.btnadd); //덧셈 | |
btn2 = findViewById(R.id.btn2); //뺄셈 | |
btn3 = findViewById(R.id.button7); //곱셈 | |
btn4 = findViewById(R.id.button9); //뺄셈 | |
//R객체에 모든 아이디가 있습니다. | |
num1 = findViewById(R.id.txtNum1); | |
num2 = findViewById(R.id.txtNum2); | |
addbtn.setOnClickListener(new View.OnClickListener(){ | |
@Override | |
public void onClick(View v) { | |
//숫자를 더한 결과를 토스트로 확인합니다. | |
String s1 = num1.getText().toString(); | |
String s2 = num2.getText().toString(); | |
//형변환 | |
int result = Integer.parseInt(s1) + Integer.parseInt(s2) ; | |
Toast.makeText(getApplicationContext(),"연산완료",Toast.LENGTH_LONG).show(); | |
System.out.println(result + " 덧셈 결과입니다."); | |
//버튼을 클릭하면 버튼클릭이 등장하고 결과는 로그에 찍힘 | |
} | |
}); | |
//뺄셈버튼에 대해서 이벤트를 지정해줄것임. | |
//람다식으로 표현합니다. 람다식은 자바버전 1.7 이후부터 사용가능하므로 모듈셋팅에서 자바 버전 셋팅이 필요합니다. | |
btn2.setOnClickListener(v -> { | |
//숫자를 더한 결과를 토스트로 확인합니다. | |
String s1 = num1.getText().toString(); | |
String s2 = num2.getText().toString(); | |
//형변환 | |
int result = Integer.parseInt(s1) - Integer.parseInt(s2) ; | |
Toast.makeText(getApplicationContext(),"연산완료", Toast.LENGTH_LONG).show(); | |
System.out.println(result + " 뺄샘 결과입니다."); | |
}); | |
//곱셈.나눗셈 이벤트 핸들러 생성 | |
Button.OnClickListener btnhandler = new Button.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String s1 = num1.getText().toString(); | |
String s2 = num2.getText().toString(); | |
int result = 0; | |
if(v.getId() == R.id.button7){ | |
result = Integer.parseInt(s1) * Integer.parseInt(s2) ; | |
}else if (v.getId() == R.id.button9){ | |
result = Integer.parseInt(s1) / Integer.parseInt(s2) ; | |
} | |
Toast.makeText(getApplicationContext(),"연산완료", Toast.LENGTH_LONG).show(); | |
System.out.println(result + " ..복잡한 연산 결과입니다."); | |
} | |
}; | |
btn3.setOnClickListener(btnhandler); | |
btn4.setOnClickListener(btnhandler); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment