2011. 3. 21. 13:31 android App
[13-D2][UI 설계] Spinner, RadioGroup, RadioButton, CheckBox
[13-D2][UI 설계] Spinner, RadioGroup, RadioButton, CheckBox
[01] 주차 요금 계산(Spinner, RadioGroup, RadioButton, CheckBox)
1. 이미지
- View --> ImageView: 이미지를 넣을 수 있는 TextView Clss
- View --> ImageView --> ImageButton: 이미지를 넣을 수 있는 버튼 클래스
2. 체크 박스
- View --> TextView --> Button --> CompoundButton
--> RadioButton --> CheckBox
- 취미같은 다중 선택 가능
3. 라디오 버튼
- View --> TextView --> Button --> CompoundButton --> RadioButton
- 성별같은 단일 선택만 가능
- RadioGroup에 의해 RadioButton의 그룹화 가능
[02] 주차 요금 계산 TextView, Button, EditText, LinearLayout)
1. 프로젝트 생성
- Project Type: Android Project
Project name: Parking_4
Application name: Parking_4
Package name: android.Parking
Create Activity(Activity name): Parking
Min SDK Version: 4
1) AndroidManifest.xml 설정
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.Parking"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".Parking"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
2. 문자열 리소스 정의
>>>>> res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Parking!</string>
<string name="app_name">주차료 계산</string>
<string name="spiTime_prompt">시간 선택</string>
<string-array name="times">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
<item>13</item>
<item>14</item>
<item>15</item>
<item>16</item>
<item>17</item>
<item>18</item>
<item>19</item>
<item>20</item>
<item>21시간 이상</item>
</string-array>
</resources>
3. View(콘트롤) 생성 및 배치
>>>>> res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/LinearLayout01"
android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id = "@+id/txt01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="시간당 금액: " android:textSize="20px" />
<EditText android:id="@+id/edtPrice"
android:layout_width="80px" android:layout_height="wrap_content"
android:maxLength = "5" android:textSize="20px"
android:text="0"/>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02"
android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content"
>
<TextView android:id = "@+id/txt02"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="경과된 시간: " android:textSize="20px" />
<Spinner android:id="@+id/spiTime"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:entries="@array/times"
android:prompt="@string/spiTime_prompt"
/>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout03"
android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content"
>
<RadioGroup
android:id="@+id/rdg01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation = "horizontal">
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="구분: " android:textSize="20px" />
<RadioButton android:text="경차" android:id="@+id/rdo01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="20px" />
<RadioButton android:text="일반" android:id="@+id/rdo02"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="20px" />
<RadioButton android:text="승합차" android:id="@+id/rdo03"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="20px" />
</RadioGroup>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout06"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="할인 선택: "
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="20px" />
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout04"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<CheckBox android:text="회원" android:id="@+id/chk01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="20px" />
<CheckBox android:text="국가 유공자" android:id="@+id/chk02"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="20px" />
<CheckBox android:text="다자녀" android:id="@+id/chk03"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="20px" />
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout05" android:background="#ffff00"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:text=" 원금: 0" android:id="@+id/txtWonkum"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="20px" android:textColor="#000000"/>
<TextView android:text=" 할인 금액: 0" android:id="@+id/txtDc"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="20px" android:textColor="#000000"/>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout06" android:background="#90ee90"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:text=" 지불 금액: 0" android:id="@+id/txtPayment"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="20px" android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
4. Class(이벤트) 작성
- 이벤트 등록은 생성자에서 불가능.
>>>>> src/android.Parking.Parking.java
package android.parking;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class Parking extends Activity implements OnClickListener{
EditText edtPrice;
Spinner spi;
RadioButton rdo01;
RadioButton rdo02;
RadioButton rdo03;
CheckBox chk01;
CheckBox chk02;
CheckBox chk03;
TextView txtWonkum;
TextView txtDc;
TextView txtPayment;
int payment = 0;
int wonkum = 0;
int dc = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edtPrice = (EditText) findViewById(R.id.edtPrice);
edtPrice.setOnClickListener(this);
spi = (Spinner)findViewById(R.id.spiTime);
spi.setOnItemSelectedListener(
new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int index = spi.getSelectedItemPosition();
// 시간당 금액
wonkum = new Integer((edtPrice.getText()).toString()).intValue();
// 선택된 시간
TextView selItem = (TextView)spi.getSelectedView();
// 선택된 시간을 정수로 변경
int _time = new Integer(selItem.getText().toString()).intValue();
// 주차시간 계산
wonkum = wonkum * _time;
display();
}
public void onNothingSelected(AdapterView<?> arg0) {
wonkum = 0;
}
}
);
rdo01 = (RadioButton)findViewById(R.id.rdo01);
rdo01.setOnClickListener(this);
rdo02 = (RadioButton)findViewById(R.id.rdo02);
rdo02.setOnClickListener(this);
rdo03 = (RadioButton)findViewById(R.id.rdo03);
rdo03.setOnClickListener(this);
chk01 = (CheckBox)findViewById(R.id.chk01);
chk01.setOnClickListener(this);
chk02 = (CheckBox)findViewById(R.id.chk02);
chk02.setOnClickListener(this);
chk03 = (CheckBox)findViewById(R.id.chk03);
chk03.setOnClickListener(this);
txtWonkum = (TextView) findViewById(R.id.txtWonkum);
txtDc = (TextView) findViewById(R.id.txtDc);
txtPayment = (TextView) findViewById(R.id.txtPayment);
display();
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.edtPrice){
wonkum = 0;
}else if(v.getId() == R.id.rdo01){
dc = (int)Math.round(wonkum * 0.5);
}else if(v.getId() == R.id.rdo02){
dc = (int)Math.round(wonkum * 0.1);
}else if(v.getId() == R.id.rdo03){
dc = (int)Math.round(wonkum * 0.0);
}
if((v.getId() == R.id.chk01) && (chk01.isChecked() == true)){
dc = dc + (int)Math.round(wonkum * 0.1);
}
if((v.getId() == R.id.chk01) && (chk01.isChecked() == false)){
dc = dc - (int)Math.round(wonkum * 0.1);
}
if((v.getId() == R.id.chk02) && (chk02.isChecked() == true)){
dc = dc + (int)Math.round(wonkum * 0.1);
}
if((v.getId() == R.id.chk02) && (chk02.isChecked() == false)){
dc = dc - (int)Math.round(wonkum * 0.1);
}
if((v.getId() == R.id.chk03) && (chk03.isChecked() == true)){
dc = dc + (int)Math.round(wonkum * 0.1);
}
if((v.getId() == R.id.chk03) && (chk03.isChecked() == false)){
dc = dc - (int)Math.round(wonkum * 0.1);
}
display();
}
public void display(){
payment = wonkum - dc;
txtWonkum.setText("원금: " + wonkum);
txtDc.setText(" 할인 금액: " + dc);
txtPayment.setText("지불 금액: " + payment);
}
}
'android App' 카테고리의 다른 글
[13-3] 메뉴 (0) | 2011.03.21 |
---|---|
[13-2-D2][UI 설계] ListView, Adapter (0) | 2011.03.21 |
[12-D2][UI 설계] EditText와 TableLayout (0) | 2011.03.21 |
[11-D2][UI 설계] 지역화(국제화), ImageView, RelativeLayout, 한글 Encoding (0) | 2011.03.21 |
[10-D2][UI 설계] Style and Theme (0) | 2011.03.21 |