Android 계산기(TableLayout)
30048 단어 ExampleCodeExampleCode
1. activity_main.xml 코드
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:layout_margin="5dp">
<TableRow
android:layout_width="match_parent">
<EditText
android:id="@+id/etNum1"
android:layout_span="4"
android:hint="숫자 입력" />
</TableRow>
<TableRow>
<EditText
android:id="@+id/etNum2"
android:layout_span="4"
android:hint="숫자 입력" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnNum0"
android:text="0"
android:layout_weight="1"
android:layout_margin="5dp"/>
<Button
android:id="@+id/btnNum1"
android:text="1"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum2"
android:text="2"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum3"
android:text="3"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum4"
android:text="4"
android:layout_weight="1"
android:layout_margin="5dp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnNum5"
android:text="5"
android:layout_weight="1"
android:layout_margin="5dp"/>
<Button
android:id="@+id/btnNum6"
android:text="6"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum7"
android:text="7"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum8"
android:text="8"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum9"
android:text="9"
android:layout_weight="1"
android:layout_margin="5dp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnAdd"
android:layout_span="4"
android:layout_margin="5dp"
android:text="더하기" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnMinus"
android:layout_span="4"
android:layout_margin="5dp"
android:text="빼기" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnMul"
android:layout_span="4"
android:layout_margin="5dp"
android:text="곱하기" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnDiv"
android:layout_span="4"
android:layout_margin="5dp"
android:text="나누기" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tvResult"
android:layout_span="4"
android:text="계산 결과 : "
android:textColor="@android:color/holo_red_light"
android:textSize="20dp"
android:layout_margin="5dp"/>
</TableRow>
</TableLayout>
2. MainActivity.java 코드
public class MainActivity extends AppCompatActivity {
private EditText etNum1, etNum2;
private Button[] btn = new Button[14];
private TextView tvResult;
private String num1, num2;
private Integer result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("테이블레이아웃 계산기");
init();
initLr();
}
public void init(){
etNum1 = findViewById(R.id.etNum1);
etNum2 = findViewById(R.id.etNum2);
tvResult = findViewById(R.id.tvResult);
btn[0] = findViewById(R.id.btnNum0);
btn[1] = findViewById(R.id.btnNum1);
btn[2] = findViewById(R.id.btnNum2);
btn[3] = findViewById(R.id.btnNum3);
btn[4] = findViewById(R.id.btnNum4);
btn[5] = findViewById(R.id.btnNum5);
btn[6] = findViewById(R.id.btnNum6);
btn[7] = findViewById(R.id.btnNum7);
btn[8] = findViewById(R.id.btnNum8);
btn[9] = findViewById(R.id.btnNum9);
btn[10] = findViewById(R.id.btnAdd);
btn[11] = findViewById(R.id.btnMinus);
btn[12] = findViewById(R.id.btnMul);
btn[13] = findViewById(R.id.btnDiv);
}
public void initLr(){
btn[10].setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
num1 = etNum1.getText().toString();
num2 = etNum2.getText().toString();
result = Integer.parseInt(num1) + Integer.parseInt(num2);
tvResult.setText("계산 결과 : " + result.toString());
return false;
}
});
btn[11].setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
num1 = etNum1.getText().toString();
num2 = etNum2.getText().toString();
result = Integer.parseInt(num1) - Integer.parseInt(num2);
tvResult.setText("계산 결과 : " + result.toString());
return false;
}
});
btn[12].setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
num1 = etNum1.getText().toString();
num2 = etNum2.getText().toString();
result = Integer.parseInt(num1) * Integer.parseInt(num2);
tvResult.setText("계산 결과 : " + result.toString());
return false;
}
});
btn[13].setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
num1 = etNum1.getText().toString();
num2 = etNum2.getText().toString();
result = Integer.parseInt(num1) / Integer.parseInt(num2);
tvResult.setText("계산 결과 : " + result.toString());
return false;
}
});
for(int i = 0; i < 10; i++){
final int index;
index = i;
btn[index].setOnClickListener(v -> {
if(etNum1.isFocused() == true){
num1 = etNum1.getText().toString() + btn[index].getText().toString();
etNum1.setText(num1);
} else if(etNum2.isFocused() == true) {
num2 = etNum2.getText().toString() + btn[index].getText().toString();
etNum2.setText(num2);
} else {
Toast.makeText(getApplicationContext(), "먼저 에디트텍스트를 선택하세요",
Toast.LENGTH_SHORT).show();
}
});
}
}
}
Author And Source
이 문제에 관하여(Android 계산기(TableLayout)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://velog.io/@ruinak_4127/Android-TableLayout-계산기
저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:layout_margin="5dp">
<TableRow
android:layout_width="match_parent">
<EditText
android:id="@+id/etNum1"
android:layout_span="4"
android:hint="숫자 입력" />
</TableRow>
<TableRow>
<EditText
android:id="@+id/etNum2"
android:layout_span="4"
android:hint="숫자 입력" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnNum0"
android:text="0"
android:layout_weight="1"
android:layout_margin="5dp"/>
<Button
android:id="@+id/btnNum1"
android:text="1"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum2"
android:text="2"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum3"
android:text="3"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum4"
android:text="4"
android:layout_weight="1"
android:layout_margin="5dp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnNum5"
android:text="5"
android:layout_weight="1"
android:layout_margin="5dp"/>
<Button
android:id="@+id/btnNum6"
android:text="6"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum7"
android:text="7"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum8"
android:text="8"
android:layout_weight="1"
android:layout_margin="5dp" />
<Button
android:id="@+id/btnNum9"
android:text="9"
android:layout_weight="1"
android:layout_margin="5dp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnAdd"
android:layout_span="4"
android:layout_margin="5dp"
android:text="더하기" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnMinus"
android:layout_span="4"
android:layout_margin="5dp"
android:text="빼기" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnMul"
android:layout_span="4"
android:layout_margin="5dp"
android:text="곱하기" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnDiv"
android:layout_span="4"
android:layout_margin="5dp"
android:text="나누기" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tvResult"
android:layout_span="4"
android:text="계산 결과 : "
android:textColor="@android:color/holo_red_light"
android:textSize="20dp"
android:layout_margin="5dp"/>
</TableRow>
</TableLayout>
public class MainActivity extends AppCompatActivity {
private EditText etNum1, etNum2;
private Button[] btn = new Button[14];
private TextView tvResult;
private String num1, num2;
private Integer result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("테이블레이아웃 계산기");
init();
initLr();
}
public void init(){
etNum1 = findViewById(R.id.etNum1);
etNum2 = findViewById(R.id.etNum2);
tvResult = findViewById(R.id.tvResult);
btn[0] = findViewById(R.id.btnNum0);
btn[1] = findViewById(R.id.btnNum1);
btn[2] = findViewById(R.id.btnNum2);
btn[3] = findViewById(R.id.btnNum3);
btn[4] = findViewById(R.id.btnNum4);
btn[5] = findViewById(R.id.btnNum5);
btn[6] = findViewById(R.id.btnNum6);
btn[7] = findViewById(R.id.btnNum7);
btn[8] = findViewById(R.id.btnNum8);
btn[9] = findViewById(R.id.btnNum9);
btn[10] = findViewById(R.id.btnAdd);
btn[11] = findViewById(R.id.btnMinus);
btn[12] = findViewById(R.id.btnMul);
btn[13] = findViewById(R.id.btnDiv);
}
public void initLr(){
btn[10].setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
num1 = etNum1.getText().toString();
num2 = etNum2.getText().toString();
result = Integer.parseInt(num1) + Integer.parseInt(num2);
tvResult.setText("계산 결과 : " + result.toString());
return false;
}
});
btn[11].setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
num1 = etNum1.getText().toString();
num2 = etNum2.getText().toString();
result = Integer.parseInt(num1) - Integer.parseInt(num2);
tvResult.setText("계산 결과 : " + result.toString());
return false;
}
});
btn[12].setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
num1 = etNum1.getText().toString();
num2 = etNum2.getText().toString();
result = Integer.parseInt(num1) * Integer.parseInt(num2);
tvResult.setText("계산 결과 : " + result.toString());
return false;
}
});
btn[13].setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1){
num1 = etNum1.getText().toString();
num2 = etNum2.getText().toString();
result = Integer.parseInt(num1) / Integer.parseInt(num2);
tvResult.setText("계산 결과 : " + result.toString());
return false;
}
});
for(int i = 0; i < 10; i++){
final int index;
index = i;
btn[index].setOnClickListener(v -> {
if(etNum1.isFocused() == true){
num1 = etNum1.getText().toString() + btn[index].getText().toString();
etNum1.setText(num1);
} else if(etNum2.isFocused() == true) {
num2 = etNum2.getText().toString() + btn[index].getText().toString();
etNum2.setText(num2);
} else {
Toast.makeText(getApplicationContext(), "먼저 에디트텍스트를 선택하세요",
Toast.LENGTH_SHORT).show();
}
});
}
}
}
Author And Source
이 문제에 관하여(Android 계산기(TableLayout)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@ruinak_4127/Android-TableLayout-계산기저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)