안드로이드 스튜디오 FrameLayout, GridLayout, LinearLayout 사용해서 앱 만들기



GridLayout은 TableLayout과 비슷하게 위젯을 표 형식으로 배치할 때 사용하는 Layout인데  


TableLayout은 할 수 없는 행 병합을 할 수 있어서 더 다양하게 만들 수 있습니다.


GridLayoutandroid:rowCount="", android:columnCount="" 속성을 사용해서 row와 column을 미리 지정할 수 있습니다.


GridLayout에 포함되어 있는 위젯에 android:layout_row="android:layout_column="속성을 지정해줘서 해당 위젯)가


몇 번째 행, 몇 번째 column에 위치할 지 지정해 줄 수 있습니다.


android:layout_rowSpan="" , android:layout_columnSpan="" 을 사용해서 몇 개의 행, 열을 합쳐서 사용할지 지정하고


android:layout_columnWeight="", android:layout_rowWeight="" 를 통해 행과 열의 비율을 지정해서 사용할 수 있습니다.




FrameLayout은 위젯을 겹처서 사용하고 싶을때 사용하는 layout입니다.


버튼이 클릭될때 화면이 전환되는 것처럼 보여주기 위해  FrameLayout안에 DatePickerTimePickerLinearLayoutGridLayout을 넣고


위젯의 visibility가 상황에 따라 변하게 설정해 주었습니다.


LinearLayout은 위젯을 수직 혹은 수평으로 배치할 수 있는 layout입니다.


수직으로 배치하고 싶으면 android:orientation="vertical" ,


수평으로 배치하고 싶으면 android:orientation="horizontal" 을 사용하면 됩니다. 







위의 앱은 GridLayout, FrameLayout, LinearLayout을 사용했습니다.


TextView, EditText, Button 외에도 SwitchChronometer, DatePicker, TimePicker를 사용했습니다.


Switch는 두 개의 상태를 표시하는 버튼입니다.


자바 파일에서 .setOnCheckedChangeListener() 이벤트 설정을 통해 상태에 따른 효과를 다르게 줄 수 있습니다.


Chronometer는 시간을 측정하는 위젯입니다.


자바 파일에서 .start()를 통해 Count를 시작 할 수 있고 .stop()을 통해 정지 시킬 수 있습니다.


만약 다시 00:00으로 reset하고 싶으면 .setBase(SystemClock.elapsedRealtime()) 를 사용합니다.


DatePicker는 날짜를 표시하는 위젯입니다.


선택된 정보를 가져오기 위해서는 getYear(), getMonth(), getDayOfMonth()를 사용하는데


DatePicker에서 선택된 정보는 int로 반환되기 때문에 String으로 사용하고 싶다면 형 변환을 해야합니다.

String year = Integer.toString(datePicker.getYear());

String month = Integer.toString(datePicker.getMonth() + 1);

String day = Integer.toString(datePicker.getDayOfMonth());


TimePicker는 시간을 표시하는 위젯입니다.


TimePicker에서 선택된 정보도 int로 반환되기 때문에 String으로 사용하고 싶다면 형 변환을 해야합니다.

String hour = Integer.toString(timePicker.getCurrentHour()) ;

String minute = Integer.toString(timePicker.getCurrentMinute());


아마 TimePicker의 getCurrentHour()와 getCurrentMinute()의 중앙에 밑줄이 그어져 있을텐데


이유는 API 23이후에는 getCurrentHour(), getCurrentMinute()가 사라지고 getHour()와 getMinute()으로 대체되기 때문입니다


저는 API 16을 기준으로 앱을 만들었기 때문에 그냥 getCurrentHour(), getCurrentMinute() 를 사용했습니다.


Button이 실행 중 작동하지 않게 하게 하려면 .setEnabled(false) 다시 작동하게 하려면 Button에 .setEnabled(true)을  사용합니다.


그리고 .setEnabled(false)로 설정된 버튼이 다시 작동하게 하기 위해서는 .setEnabled(true) 값을 설정해 주면 됩니다.



위 앱의 모든 코드는 GitHub에서 볼 수 있습니다!


https://github.com/Ywook/Android.Practice2_2






안드로이드 스튜디오 TableLayout 사용해서 학점 계산 앱 만들기



TableLayout은 위젯을 표 형식으로 배치할 때 사용하는 layout입니다.


TableRow로 테이블에 행을 하나씩 추가하고, 추가한 행에 위젯들을 배치합니다.


TableLayout은 행 병합은 할 수 없만 속성을 이용해서 열 병합을 할 수 있습니다.


android:layout_span="n" 은 n개의 column을 병합해서 한 개의 column으로 표시하라는 뜻입니다. 


android:stretchColumns="" 은 column을 지정해서 해당 column이 row를 가득 채우도록 하는 속성입니다.


"" 안에 0, 1, 2 이런 식으로 다수의 column을 지정해 줄 수도 있고 모든 Column에게 적용하고 싶으면 *를 넣어주면 됩니다.


android:visibility="visible" 은 화면에 표시하라는 속성

android:visibility="invisible" 은 화면에서 보이지 않게 하라는 속성

android:visibility="gone" 은 화면에서 보이지도 않고 공간도 차지하지 않게하는 속성입니다.


즉, invisibler과 gone은 둘 다 화면에서 보이지 않지만 invisible은 공간은 차지하고 있고 gone 공간도 차지하지 않게 한다는 뜻입니다.


visiblity 속성을 지정해주지 않았다면 기본으로 VISIBLE이 적용되어 화면에 표시됩니다.


xml 파일이 아닌 자바파일에서 적용을 하고 싶다면 적용하고 싶은 뷰(View)에 


.setVisibility(View.VISIBLE);

.setVisibility(View.INVISIBLE);

.setVisibility(View.GONE);


중 원하는 속성을 적용 시키면 됩니다.


ImageView는 이미지를 표시할 수 있는 위젯입니다. 


이미지를 설정해주기 위해서 xml 파일에서 android:src=""을 사용하거나 자바 파일에서 .setImageResource()를 사용해주면 됩니다.


text의 Color를 바꾸고 싶다면 android:textColor="#FFFFFF를 이용해서 색을 변경할 수 있습니다.(#FFFFFF은 흰색입니다.)


배경색을 바꾸고 싶다면 android:background=""를 이용하면 됩니다!


밑에 앱은 TextView의 textColor와 TableLayout의 background의 색을 설정하고


ImageView, EditText, Button, TextView를 사용하여 만들었습니다.


  




위 앱의 전체 코드는 gitHub에서 볼 수 있습니다.


https://github.com/Ywook/Android.Practice2_1

안드로이드 스튜디오 기본 위젯(Widget)을 사용한 앱 만들기 (2)


안드로이드 스튜디오 기본 위젯(Widget)을 사용한 앱 만들기 (1)



이전글에서 추가한 Actvity를 차례대로 완성해보겠습니다.


아래 코드는 사과가격 계산의 xml입니다.

여기서 EditText의  android:hint="는 글자를 입력하지 않을때 " "안의 글자가 흐릿하게 보이게하는 속성입니다.

<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_margin="20dp"
android:layout_height="match_parent"
tools:context="examples.com.myapplication2.Main2Activity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="사과의 가격은?"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="숫자로 입력"
android:id="@+id/editText"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="사과의 개수는?"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="숫자로 입력"
android:id="@+id/editText2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="총 금액 계산하기"/>
</LinearLayout>



사과가격 계산의 자바 소스입니다.

여기서 setTitle("")은 액션바에 나타나는 글자를 설정할 수 있는 메소드입니다.


EditText에 작성되어 있는 text는 getText()로 얻을 수 있는데 반환 형식이 Text이므로 String으로 받기 위해

.toString()을 통해 형변환을 시켜줍니다.


Toast.makeText().show(); 통해 화면 하단에 메세지를 띄울 수 있습니다.

첫번째 인자로 현재 Activity가 들어가고 

두번째 인자로 출력하고 싶은 내용

세번째 인자로 Toast.LENGTH_SHORT, Toast.LENGTH_LONG을 선택해서 보여주는 시간을 설정할 수 있습니다. 


package examples.com.myapplication2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {
EditText e1, e2;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setTitle("사과가격 계산기");
init();
}

void init(){
e1 = (EditText)findViewById(R.id.editText);
e2 = (EditText)findViewById(R.id.editText2);
b1 = (Button)findViewById(R.id.btn);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String price = e1.getText().toString();
String count = e2.getText().toString();
int result = Integer.parseInt(price)*Integer.parseInt(count);

Toast.makeText(getApplicationContext(),"사과의 가격은 " + result, Toast.LENGTH_SHORT).show();
}
});
}

} 



실행화면입니다.





사과가격 계산하기와 같은 방법으로 만든 나이계산기의 xml 코드입니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_margin="20dp"
android:layout_height="match_parent"
tools:context="examples.com.myapplication2.Main3Activity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="태어난 년도를 입력하세요."/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="숫자를 입력하세요."
android:id="@+id/age_edit"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="나이계산"
android:id="@+id/age_btn"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="나이를 입력하세요."/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="숫자를 입력하세요."
android:id="@+id/age_edit2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/age_btn2"
android:text="태어난 해 계산"/>

</LinearLayout> 



나이계산기의 자바 소스입니다.
package examples.com.myapplication2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main3Activity extends AppCompatActivity {
int YEAR = 2017;
EditText e1, e2;
Button btn1, btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
setTitle("나이계산기");
init();
}

public void init(){
btn1 = (Button) findViewById(R.id.age_btn);
btn2 = (Button) findViewById(R.id.age_btn2);
e1 = (EditText)findViewById(R.id.age_edit);
e2 = (EditText)findViewById(R.id.age_edit2);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String temp = e1.getText().toString();
int age = YEAR - Integer.parseInt(temp) + 1;
Toast.makeText(getApplicationContext(),"당신의 나이는 " + age + "세 입니다.",Toast.LENGTH_SHORT).show();
}
});

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String temp = e2.getText().toString();
int result = YEAR - Integer.parseInt(temp) +1;
Toast.makeText(getApplicationContext(),"당신의 태어난 해는 "+result+"년 입니다.",Toast.LENGTH_SHORT).show();
}
});
}

} 



실행화면입니다.




나이계산기와 동일한 방법으로 만든 온도변환기 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:orientation="vertical"
android:layout_margin="20dp"
android:layout_height="match_parent"
tools:context="examples.com.myapplication2.Main3Activity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="섭씨 온도를 입력하세요."/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="온도를 입력하세요."
android:id="@+id/h2_edit"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="화씨계산"
android:id="@+id/h2_btn"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="화씨 온도를 입력하세요."/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="온도를 입력하세요."
android:id="@+id/h2_edit2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/h2_btn2"
android:text="섭씨계산"/>

</LinearLayout> 


온도변환기의 자바 소스입니다.

 package examples.com.myapplication2;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main4Activity extends AppCompatActivity {
EditText e1, e2;
Button btn1, btn2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
setTitle("온도변환기");
init();
}
public void init(){
btn1 = (Button) findViewById(R.id.h2_btn);
btn2 = (Button) findViewById(R.id.h2_btn2);
e1 = (EditText)findViewById(R.id.h2_edit);
e2 = (EditText)findViewById(R.id.h2_edit2);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String temp = e1.getText().toString();
double result = Integer.parseInt(temp)*1.8 + 32;
Toast.makeText(getApplicationContext(),"화씨 온도는 " + result + "도 입니다.",Toast.LENGTH_SHORT).show();
}
});

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String temp = e2.getText().toString();
double result = (Integer.parseInt(temp)-32)/(1.8);
Toast.makeText(getApplicationContext(),"섭씨 온도는 "+result+"도 입니다.",Toast.LENGTH_SHORT).show();
}
});
}
}



실행화면입니다.




레스토랑 주문의 xml 코드 입니다.

위의 코드에서 android:weightSum과 android:layout_weight 라는 새로운 속성이 추가되었습니다.

weightSum을 통해 비율의 합을 정해주고 layout_weight를 통해 비율을 나눠 줄 수 있습니다.

예를 들어 layout안의 두 개의 뷰(View)를 4:3으로 나눠서 사용하고 싶다면 layout에 weightSum을 7을 주고 

두개의 뷰(View)의 layout_weight를  각각 4, 3을 주고 

가로로 비율을 주고 싶으면 width에 0dp, 세로로 비율을 주고 싶으면 height에 0dp를 줘서 사용할 수 있습니다.


그리고 android:textStyle="" 를 통해 뷰(View) 에 들어있는 텍스트의 스타일을 변경해 줄 수 있습니다. 

안드로이드 스튜디오는는 bold, italic, normal을 기본으로 지원하는데 CheckBox와 Button의 텍스트를 진하게

보여주기 위해서 bold 속성을 추가했습니다.

<?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"
android:layout_margin="20dp"
tools:context="examples.com.myapplication2.Main5Activity">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="7"
android:orientation="horizontal">


<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="피자(15000원)" />

<EditText
android:id="@+id/h3_edit"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="개수 입력"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="7"
android:orientation="horizontal">


<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="스파게티(13000원)" />

<EditText
android:id="@+id/h3_edit2"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="개수 입력"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="7"
android:orientation="horizontal">


<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="샐러드(9000원)" />

<EditText
android:id="@+id/h3_edit3"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="개수 입력"/>

</LinearLayout>

<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:text="멤버쉽 카드 있음(10%할인)" />

<Button
android:id="@+id/h3_btn"
android:layout_width="match_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:text="금액 계산하기" />

<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="7"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="주문개수"/>

<TextView
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:id="@+id/h3_text"
android:text="0개" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="7"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="주문개수"/>

<TextView
android:layout_width="0dp"
android:id="@+id/h3_text2"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="0개" />
</LinearLayout>


</LinearLayout> 



레스토랑 주문의 자바 소스입니다.

CheckBox의 isChecked()는 CheckBox가 Check되어 있으면 true 아닐경우 false를 반환하는 메소드입니다.

true일 경우 10퍼센트 할인된 가격을 출력합니다.

package examples.com.myapplication2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;


public class Main5Activity extends AppCompatActivity {
EditText e1, e2, e3;
Button btn;
CheckBox checkBox;
TextView text1,text2;
int PIZZA = 15000, SPAGHETTI = 13000, SALAD = 9000;
double DISCOUNT = 0.9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
setTitle("레스토랑 주문");

init();

}



void init() {
e1 = (EditText) findViewById(R.id.h3_edit);
e2 = (EditText) findViewById(R.id.h3_edit2);
e3 = (EditText) findViewById(R.id.h3_edit3);

btn = (Button) findViewById(R.id.h3_btn);

checkBox = (CheckBox) findViewById(R.id.checkBox);

text1 = (TextView) findViewById(R.id.h3_text);
text2 = (TextView) findViewById(R.id.h3_text2);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int t1 = Integer.parseInt(e1.getText().toString());
int t2 = Integer.parseInt(e2.getText().toString());
int t3 = Integer.parseInt(e3.getText().toString());

int result1 = t1 + t2 + t3;
int result2 = t1 * PIZZA + t2 * SPAGHETTI + t3 * SALAD;


if (checkBox.isChecked()) {
result2 = (int) (result2 * DISCOUNT);
}

text1.setText(result1+"개");
text2.setText(result2+"원");
}
});
}
}

실행화면입니다.




계산기의 xml 소스 입니다.

레스토랑 주문하기와 비슷하게 layout_weight를 사용하여 만들었습니다.

<?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:orientation="vertical"
android:layout_margin="20dp"
android:layout_height="match_parent"
tools:context="examples.com.myapplication2.Main6Activity">

<LinearLayout
android:orientation="horizontal"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1.35"
android:layout_height="wrap_content"
android:text="숫자 1 "/>
<EditText
android:layout_width="0dp"
android:layout_weight="8.65"
android:layout_height="wrap_content"
android:hint="정수 입력"
android:id="@+id/h4_edit"/>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1.35"
android:layout_height="wrap_content"
android:text="숫자 2 "/>
<EditText
android:layout_width="0dp"
android:layout_weight="8.65"
android:layout_height="wrap_content"
android:hint="정수 입력"
android:id="@+id/h4_edit2"/>
</LinearLayout>

<Button
android:id="@+id/h4_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="더하기" />

<Button
android:id="@+id/h4_btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="빼기" />

<Button
android:id="@+id/h4_btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="곱하기" />

<Button
android:id="@+id/h4_btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="나누기" />

</LinearLayout> 



계산기의 자바 소스입니다.

EditText에 글을 적지 않았을 때는 null이 반환되고 글을 적었다 지웠을때는 빈 String인 ""이 반환됩니다.

따라서 EditText가 비었는지 체크하는 isEmpty() 메소드를 하나 만들고

EditText가 비었을 경우 Toast 메세지를 출력하고 입력이 되지 않은 곳에 .requsetFocus()를 사용하여 포커스를 줍니다.


package examples.com.myapplication2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main6Activity extends AppCompatActivity {
EditText e1, e2;
Button btn1, btn2, btn3, btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
setTitle("계산기");

init();
}

void init(){
e1 = (EditText)findViewById(R.id.h4_edit);
e2 = (EditText)findViewById(R.id.h4_edit2);

btn1 = (Button)findViewById(R.id.h4_btn);
btn2 = (Button)findViewById(R.id.h4_btn2);
btn3 = (Button)findViewById(R.id.h4_btn3);
btn4 = (Button)findViewById(R.id.h4_btn4);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s1 = e1.getText().toString();
String s2 = e2.getText().toString();

if(emptyCheck(s1, s2));
else{
int result = Integer.parseInt(s1)+Integer.parseInt(s2);
Toast.makeText(getApplicationContext(), "더하기 계산 결과는 "+result+"입니다",Toast.LENGTH_SHORT).show();
}
}
});

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s1 = e1.getText().toString();
String s2 = e2.getText().toString();

if(emptyCheck(s1, s2));
else{
int result = Integer.parseInt(s1)-Integer.parseInt(s2);
Toast.makeText(getApplicationContext(), "더하기 빼기 결과는 "+result+"입니다",Toast.LENGTH_SHORT).show();
}

}
});

btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s1 = e1.getText().toString();
String s2 = e2.getText().toString();

if(emptyCheck(s1,s2));
else{
int result = Integer.parseInt(s1)*Integer.parseInt(s2);
Toast.makeText(getApplicationContext(), "곱하기 계산 결과는 "+result+"입니다",Toast.LENGTH_SHORT).show();
}

}
});

btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s1 = e1.getText().toString();
String s2 = e2.getText().toString();

if(emptyCheck(s1,s2));
else{
double result = (double)Integer.parseInt(s1)/Integer.parseInt(s2);
Toast.makeText(getApplicationContext(), "나누기 계산 결과는 "+result+"입니다",Toast.LENGTH_SHORT).show();
}

}
});

}
boolean isEmpty(String s){
return s == null || s.equals("");
}

boolean emptyCheck(String s1, String s2){
if(isEmpty(s1)){
Toast.makeText(getApplicationContext(),"값을 입력하세요.",Toast.LENGTH_SHORT).show();
e1.requestFocus();
return true;
}else if(isEmpty(s2)){
Toast.makeText(getApplicationContext(),"값을 입력하세요.",Toast.LENGTH_SHORT).show();
e2.requestFocus();
return true;
}
return false;
}

} 


실행화면입니다.





위의 모든 코드들은 깃허브(gitHub)에서 볼 수 있습니다!

https://github.com/Ywook/MyApplication2


+ Recent posts