xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/Edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="숫자1">
</EditText>
<EditText
android:id="@+id/Edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="숫자2">
</EditText>
<Button
android:id="@+id/BtnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="더하기"
android:onClick="AddClick">
</Button>
<Button
android:id="@+id/BtnSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="뺴기"
android:onClick="SubClick">
</Button>
<Button
android:id="@+id/BtnMulti"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="곱하기"
android:onClick="MultiClick">
</Button>
<Button
android:id="@+id/BtnDiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="나누기"
android:onClick="DivClick">
</Button>
<TextView
android:id="@+id/TextResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#FF0000"
android:text="계산 결과 : "
>
</TextView>
</LinearLayout>
</LinearLayout>
java
package com.example.homework2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText edit1, edit2;
TextView textResult;
String num1, num2, tmp;
Integer result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("간단한 계산기");
edit1 = (EditText)findViewById(R.id.Edit1);
edit2 = (EditText)findViewById(R.id.Edit2);
textResult = (TextView)findViewById(R.id.TextResult);
}
// 덧셈 method
public void AddClick(View target)
{
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if ((isInteger(num1) & isInteger(num2))==1)
{
result = Integer.parseInt(num1) + Integer.parseInt(num2);
tmp = "계산 결과 : " + result.toString();
textResult.setText(tmp);
}
else
{
Toast.makeText(getApplicationContext(), "No Number", Toast.LENGTH_LONG).show();
}
}
// 뺄셈 method
public void SubClick(View target)
{
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if ((isInteger(num1) & isInteger(num2))==1)
{
result = Integer.parseInt(num1) - Integer.parseInt(num2);
tmp = "계산 결과 : " + result.toString();
textResult.setText(tmp);
}
else
{
Toast.makeText(getApplicationContext(), "No Number", Toast.LENGTH_LONG).show();
}
}
// 곱셈 method
public void MultiClick(View target)
{
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if ((isInteger(num1) & isInteger(num2))==1)
{
result = Integer.parseInt(num1) * Integer.parseInt(num2);
tmp = "계산 결과 : " + result.toString();
textResult.setText(tmp);
}
else
{
Toast.makeText(getApplicationContext(), "No Number", Toast.LENGTH_LONG).show();
}
}
// 나눗셈 method
public void DivClick(View target)
{
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
if ((isInteger(num1) & isInteger(num2))==1)
{
result = Integer.parseInt(num1) / Integer.parseInt(num2);
tmp = "계산 결과 : " + result.toString();
textResult.setText(tmp);
}
else
{
Toast.makeText(getApplicationContext(), "No Number", Toast.LENGTH_LONG).show();
}
}
// integer type method
static int isInteger(String s) {
try {
Integer.parseInt(s);
return 1;
} catch(NumberFormatException e) {
return 0;
}
}
}
EditText
// EditText
EditText edit = (EditText)findViewById(R.id.Edit);
// return string
edit.getText().toString();
TextView
// TextView
text = (TextView)findViewById(R.id.Text);
text.setText(edittext.getText());
developer.android.com/reference/android/widget/EditText
developer.android.com/reference/android/widget/TextView
728x90
반응형
'Flutter > Android' 카테고리의 다른 글
[안드로이드] 6. Activity and Intent (0) | 2021.01.25 |
---|---|
[안드로이드] 5. Option Menu (0) | 2021.01.25 |
[안드로이드] 3. LinearLayout (0) | 2021.01.24 |
[안드로이드] 2. Buttons (0) | 2021.01.24 |
[안드로이드] 1. Toast Message 와 Log Message (0) | 2021.01.24 |