우선 레이아웃에 버튼을 추가합니다. 기존 TextView를 Button으로 바꾸었습니다.
res/layout/activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 | < linearlayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "horizontal" > < button android:id = "@+id/button1" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "Next Activity" > </ button > </ linearlayout > |
그러면 버튼이 뜹니다.
그리고 소스에서 OnClickListener를 추가합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class MainActivity extends Activity implements android.view.View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button b = (Button)findViewById(R.id.button1); b.setOnClickListener( this ); } @Override public void onClick(View v) { Intent i = new Intent( this , NextActivity. class ); startActivity(i); } } |
findViewById 를 이용하여 xml 파일에 설정된 id로 설정합니다.
그리고 setOnClickListener 로 현재 클래스를 설정합니다.
그리고 그렇게 하기위해서는 현재클래스에서 implements 를 이용하여 android.view.View.OnClickListener 를 구현해 주어야합니다.
android.view.View.OnClickListener 인터페이스는 onClick 메서드를 구현해야합니다.
그리고 해당 메서드에 다음 activity를 실행하는 코드를 추가합니다.
Intent i = new Intent(this, NextActivity.class );
intent의 첫번째 변수는 context이고 두번째는 실행될 class를 설정합니다.
그리고 startActivity 로 실행을합니다.
NextActivity.java내용입니다.
1 2 3 4 5 6 7 | public class NextActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main2 ); } } |
res/layout/activity_main2.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 | < linearlayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "horizontal" > < textview android:id = "@+id/textView1" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "Hello Activity2" android:textappearance = "?android:attr/textAppearanceLarge" > </ textview > </ linearlayout > |
그리고 마지막으로 AndroidManifest.xml 에 신규 activity를 설정해줍니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!--?xml version="1.0" encoding="utf-8"?--> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.shin.shinexample" android:versioncode = "1" android:versionname = "1.0" > < uses-sdk android:minsdkversion = "8" android:targetsdkversion = "21" > < application android:allowbackup = "true" android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" > < activity android:name = ".MainActivity" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" > < category android:name = "android.intent.category.LAUNCHER" > </ category ></ action ></ intent-filter > </ activity > < activity android:name = "com.shin.shinexample.NextActivity" ></ activity > </ application > </ uses-sdk ></ manifest > |
<activity android:name="com.shin.shinexample.NextActivity"></activity> 를 추가합니다.
그리고 실행을 합니다~
'안드로이드' 카테고리의 다른 글
[안드로이드 강좌 예제 따라하기 5] 계산기만들기 - 계산부분(2/2) (2) | 2014.11.04 |
---|---|
[안드로이드 강좌 예제 따라하기 4] 계산기만들기 - 틀만들기(1/2) (2) | 2014.11.04 |
[안드로이드 강좌 예제 따라하기 2] 기존 프로젝트 불러오기 (0) | 2014.11.04 |
티스토리 블로그 자바소스 코드 넣기 (0) | 2014.11.02 |
[안드로이드 강좌 예제 따라하기 1] 기본틀 클릭 가능한 만들어 메뉴만들기 (0) | 2014.11.02 |