2011. 3. 21. 12:25 android App
[04-D1] 간단한 Application을 통한 개발 환경 테스트, 안드로이드 Application 구조
[04-D1] 간단한 Application을 통한 개발 환경 테스트, 안드로이드 Application 구조
[[[ 한글 문자열뒤에 공백이나 '.'을 출력해야 한글이 정상적으로 출력 될수 있으나
불규칙적임. 따라서 이문제는 지역화에서 해결해야함. 문자의 양쪽에 공백을 넣는 것도 하나의 해결 방법 임]]
[01] 간단한 Application을 통한 개발 환경 테스트
1. 프로젝트 생성
- Project Type: Android Project
Project name: Test_Hello_4 <------또는 SDK 2.1은 Test_Hello_7
Application name: Test_Hello_4
Package name: android.test <------ 패키지명은 2단 이상 지정, 단일 패키지 안됨.
Create Activity(Activity name): HelloAct <------- 클래스명으로 쓰임으로 첫자를 대문자로 지정
Min SDK Version: 4 <------ API 번호
2. project --> Properties --> Android --> Android 1.6 다시 체크 후 Apply
3. 실행
[Run as --> Android Application] emulator 출력후 잠시 후
화면에서 [menu]버튼 클릭
* emulator는 loading속도가 느림으로 계속 실행합니다.
4. 출력 문자열의 변경
C:/workspace_android/Hello/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">안녕하세요? 안드로이드 입니다.</string>
<string name="app_name">Hello~ Android</string>
</resources>
[02] Application의 구조
1. 환경 설정
1) AndroidManifest.xml
- 프로젝트의 구성 정보를 가지고 있음.
2) application의 빌드 정보가 들어 있슴.
2. src/*.java
- 자바 클래스 선언
3. gen/R.java
- 프로젝트에서 사용되는 View 및 각종 리소스를 16진수의 정수 형태로 관리함, 툴이 자동으로
관리하며 개발자는 관여하지 않음.
4. assets
- 리소스 폴더로 비디오, 오디오등의 대용량 파일을 저장함.
5. res/drawable
- 이미지 파일을 저장하며, 어플리케이션의 기본 아이콘, 각각의 해상도별로 자동으로 적용되는
이미지 폴더가 있으며, png형식을 권장함.
6. res/layout/main.xml
- Java와 XML을 이용하여 구현 가능.
- 복잡한 레이아웃의 구현은 코드로 구현이 어렵기 때문에 XML 사용을 절대 권장.
- 디자이너와 개발자가 분리 작업을 할 수 있어 전문성을 강화할 수 있음.
- Activity의 디자인을 담당, 파일명은 영어 소문자와 숫자만 가능.
7. res/values/strings.xml
- 프로젝트에서 사용하는 문자열을 저장.
9. bin
- apk 파일이 있으며 핸드폰에 설치할 수 있는 패키지임.
adb install Test_Hello.apk
adb uninstall Test_Hello.apk
adb install -r Test_Hello.apk
[03] 버튼을 누르면 현재 시간을 알려주는 Application의 작성
1. 프로젝트 생성
- Project Type: Android Project
Project name: Test_Date_4
Application name: Activity Test
Package name: android.test.date <-- 2단이상의 패키지 사용
Create Activity(Activity name): TestActivity
Min SDK Version: 4
2. resource의 선언
[res --> values --> strings.xml]
Resources Elements - String
String Name: msg, Value: Activity 실습입니다.
Resources Elements - String
Name: btnDate, Value: 버튼을 클릭하세요.
>>>>> res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Activity Test2</string>
<string name="msg">TestActivity 입니다.</string>
<string name="btnDate">버튼을 클릭하세요.</string>
</resources>
3. Layout의 선언
[res --> layout --> main.xml]
Button 추가
Properties View에서 Text속성에 문자열 자원 할당
Id: @+id/btn01, Text: @string/btnDate
>>>>> 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"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txvMsg"
android:text="@string/msg" android:textSize="10pt"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDate"
android:text="@string/btnDate" android:textSize="10pt"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnClear" android:text=" 삭제 "
android:textSize="10pt" android:textColor="#00AA00"></Button>
</LinearLayout>
4. Activity class
- JDK 1.5는 @Override annotation을 지원하지 않음으로 JDK 1.6으로 compiler를
변경할 것.
>>>>> src/android/test/date/TestActivity.java
package android.test.date;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TestActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// public View findViewById (int id)
// 버튼과 이벤트 핸들러(클릭 기능 처리 메소드) 연결
// 버튼을 찾아야 함.
Button btnDate = (Button)findViewById(R.id.btnDate);
// 버튼이 클릭 되었을 때 버튼 클릭 소리를 듣고 처리할
// 객체를 콘트롤에 등록
// this == TestActivity
// TestActivity가 OnClickListener를 구현했음으로 this로 선언
btnDate.setOnClickListener(this); // this = TestActivity
Button btnClear = (Button)findViewById(R.id.btnClear);
btnClear.setOnClickListener(this);
}
@Override
/**
* v: 이벤트가 발생한 View콘트롤이 들어가 있음.
*/
public void onClick(View v) {
// Toast.makeText(this, "버튼이 클릭 되었습니다.", Toast.LENGTH_LONG).show();
// Toast t = Toast.makeText(this, "버튼이 클릭 되었습니다.", Toast.LENGTH_LONG);
// t.show();
/*
TextView txvMsg = (TextView)findViewById(R.id.txvMsg);
String date = new Date().toLocaleString();
txvMsg.setText(date);
*/
if (v.getId() == R.id.btnDate){
TextView txvMsg = (TextView)findViewById(R.id.txvMsg);
String date = new Date().toLocaleString();
txvMsg.setText(date);
}else if(v.getId() == R.id.btnClear){
Toast.makeText(this, "날짜 삭제", Toast.LENGTH_SHORT).show();
TextView txvMsg = (TextView)findViewById(R.id.txvMsg);
txvMsg.setText("");
}
}
}
5. 참조 관계
- id 속성의 특징
@ : id는 @기호로 시작 되어야 함.
@+id/txvName : 리소스(View)에 새로운 아이디 선언.
@id/txvName : 기존에 선언된 리소스를 참조 할 경우.
@android/id:list: Android에서 시스템에서 사용하는 지정된 id일 경우
TestActivity.java
setContentView(R.layout.main)
|
|
gen/android.test.date.R.java
R.layout.main
|
|
res/layout/main.xml
android:text="@string/msg"
|
|
res/values/strings.xml
'android App' 카테고리의 다른 글
[06-D2] Emulator, 한글 키패드 설정 & Android App의 수동 설치 (0) | 2011.03.21 |
---|---|
[05-D1] 폰 환경 설정 및 테스트(Window XP, Ubuntu Linux 10.04) (0) | 2011.03.21 |
[03-D1] Ubuntu 10.04 리눅스에서의 개발 환경 설정(ADT, SDK, AVD) (0) | 2011.03.21 |
[02-D1] Ubuntu 10.04 리눅스에서의 개발 환경 설정(JDK 6, Eclipse Galileo) (0) | 2011.03.21 |
[01-D1] 안드로이드의 개요, Eclipse ADT(Android Development Tools), AVD (0) | 2011.03.21 |