작동 영상
< MainActivity.java >
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Button button10;
Button button11;
Button button0;
Button button13;
int x1, x2;
char op;
int result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(); //동기화
click(); //클릭
}
private void findViewById(){
button=findViewById(R.id.button);
button2=findViewById(R.id.button2);
button3=findViewById(R.id.button3);
button4=findViewById(R.id.button4);
button5=findViewById(R.id.button5);
button6=findViewById(R.id.button6);
button7=findViewById(R.id.button7);
button8=findViewById(R.id.button8);
button9=findViewById(R.id.button9);
button10=findViewById(R.id.button10);
button11=findViewById(R.id.button11);
button0=findViewById(R.id.button0);
button13=findViewById(R.id.button13);
}
private void click()
{
editText = findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString(); //기존 문자열 알기
s += button.getText().toString(); //버튼에서 얻어온 텍스트를 스트링으로
editText.setText(s); //editText에 셋(매개변수 : 스트링)
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString(); //기존 문자열 알기
s += button2.getText().toString();
editText.setText(s);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString(); //기존 문자열 알기
s += button3.getText().toString();
editText.setText(s);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString(); //기존 문자열 알기
s += button4.getText().toString();
editText.setText(s);
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString(); //기존 문자열 알기
s += button5.getText().toString();
editText.setText(s);
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString(); //기존 문자열 알기
s += button6.getText().toString();
editText.setText(s);
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString(); //기존 문자열 알기
s += button7.getText().toString();
editText.setText(s);
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString(); //기존 문자열 알기
s += button8.getText().toString();
editText.setText(s);
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString(); //기존 문자열 알기
s += button9.getText().toString();
editText.setText(s);
}
});
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString(); //기존 문자열 알기
s += button0.getText().toString();
editText.setText(s);
}
});
//연산자
button10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
x1 = Integer.parseInt(editText.getText().toString());
op = '+';
editText.setText("");
}
});
button11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
x1 = Integer.parseInt(editText.getText().toString());
op = '-';
editText.setText("");
}
});
button13.setOnClickListener(new View.OnClickListener() { // = 연산자 계산
@Override
public void onClick(View v) {
x2 = Integer.parseInt(editText.getText().toString());
switch(op)
{
case '+':
result = x1 + x2;
editText.setText(Integer.toString(result));
break;
case '-':
result = x1 - x2;
editText.setText(Integer.toString(result));
break;
}
}
});
}
}
< activity_main.xml >
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<Button
android:id="@+id/button5" //아이디 생성
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:text="5"
app:layout_constraintStart_toEndOf="@+id/button4"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="28dp"
android:layout_marginTop="12dp"
android:text="4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<Button
android:id="@+id/button6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="20dp"
android:layout_marginTop="12dp"
android:text="6"
app:layout_constraintStart_toEndOf="@+id/button5"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="12dp"
android:layout_marginTop="20dp"
android:text="2"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="12dp"
android:layout_marginTop="20dp"
android:text="8"
app:layout_constraintStart_toEndOf="@+id/button7"
app:layout_constraintTop_toBottomOf="@+id/button5" />
<Button
android:id="@+id/button7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="28dp"
android:layout_marginTop="20dp"
android:text="7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />
<Button
android:id="@+id/button9"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="24dp"
android:layout_marginTop="20dp"
android:text="9"
app:layout_constraintStart_toEndOf="@+id/button8"
app:layout_constraintTop_toBottomOf="@+id/button6" />
<Button
android:id="@+id/button11"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="12dp"
android:layout_marginTop="16dp"
android:text="-"
app:layout_constraintStart_toEndOf="@+id/button10"
app:layout_constraintTop_toBottomOf="@+id/button8" />
<Button
android:id="@+id/button0"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:text="0"
app:layout_constraintStart_toEndOf="@+id/button11"
app:layout_constraintTop_toBottomOf="@+id/button9" />
<Button
android:id="@+id/button10"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="28dp"
android:layout_marginTop="16dp"
android:text="+"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button7" />
<Button
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="24dp"
android:layout_marginTop="20dp"
android:text="3"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="28dp"
android:layout_marginTop="20dp"
android:text="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button13"
android:layout_width="338dp"
android:layout_height="68dp"
android:layout_marginStart="28dp"
android:layout_marginTop="12dp"
android:text="="
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button11" />
<EditText
android:id="@+id/editText"
android:layout_width="343dp"
android:layout_height="73dp"
android:layout_marginStart="28dp"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textStyle="bold"
android:gravity="right"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
'Front-end > Android (안드로이드 앱 개발)' 카테고리의 다른 글
Android studio (안드로이드 스튜디오) 플래그먼트 생성 2, create Fragment (0) | 2020.02.25 |
---|---|
Android studio(안드로이드 스튜디오) Fragment 플래그먼트와 버튼 적용 (0) | 2020.02.24 |
Android studio : 이미지 넣기, 앱 이미지 넣기 (0) | 2020.02.17 |
Android studio : 애뮬레이터 에러 (0) | 2020.01.20 |
Android studio : View - LinearLayout 응용 (0) | 2020.01.10 |
댓글