Chap4. 인터페이스 기초 01.View
패키지명 : MyApplication2
각각 id를 부여하고 onClick메소드를 연결해준다.
버튼을 누르면
-> EditText의 값 읽어오기
-> 누른 버튼의 값을 읽어온 값에 추가
-> 추가된 값을 EditText에 출력
= 이것이 onClick메소드에서 실행되어야 한다.
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.editText);
String str = editText.getText().toString();
//EditText의 값을 String으로 읽어오기
switch (v.getId()) {
case R.id.btnOne :
str += "1";
editText.setText(str);
break;
case R.id.btnTwo :
str += "2";
editText.setText(str);
break;
case R.id.btnThree :
str += "3";
editText.setText(str);
break;
}
}
// str += "n"; 으로 누른 버튼의 값을 str값에 추가
// editText.setText(str); 로 추가된 값을 editText에 추가
공백없는 문자열 사용
case R.id.btnClear :
editText.setText("");
break;
package ddwucom.mobile.test0426_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.editText);
String str = editText.getText().toString();
switch (v.getId()) {
case R.id.btnOne :
str += "1";
editText.setText(str);
break;
case R.id.btnTwo :
str += "2";
editText.setText(str);
break;
case R.id.btnThree :
str += "3";
editText.setText(str);
break;
case R.id.btnClear :
editText.setText("");
break;
}
}
}
<?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">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/btnOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ONE"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/btnTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TWO"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnOne"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/btnThree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="THREE"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnTwo"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/btnClear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CLEAR"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnThree"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>