안드로이드 스튜디오 기본 위젯(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