본문 바로가기

프로그램/ANDROID

안드로이드 어플 개발 / 안드로이드 페이지 이동 방법/안드로이드 화면 이동 방법

반응형

안드로이드 어플 개발 / 안드로이드 페이지 이동 방법/안드로이드 화면 이동 방법

 

우선 기본적인 activity_main.xml 화면에서 버튼을 누르면 다른 menu.xml 라는 화면으로 이동하는 걸 해보려고 합니다.

 

1. activity_main.xml 화면에 버튼을 생성합니다.

 

<RelativeLayout 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" >

<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/start_button"
tools:context=".MainActivity" />

</RelativeLayout>

 

2. strings.xml 에 버튼에 들어갈 start_button 버튼에 'START' 라고 작성해줍니다.

 

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">sudoku</string>
<string name="action_settings">Settings</string>
<string name="start_button">START</string>
<string name="new_button">NEW</string>
<string name="continue_button">CONTINUE</string>

</resources>

 

여기까지가 버튼을 세팅해준겁니다.

 

3. 버튼을 눌렀을 때 이동할 수 있도록 설정 (OnClickListener 상속받아옵니다. 강조한 부분을 추가)

 

public class MainActivity extends Activity implements OnClickListener

{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //화면 xml 명을 써준다.

Button startBtn = (Button) this.findViewById(R.id.startBtn); // 화면 activity_main.xml 의 버튼을 startBtn 을 세팅

startBtn.setOnClickListener(this); // 버튼 선택시 아래의 onClick 함수 호출


}


public void onClick(View arg0) {

// TODO Auto-generated method stub

// String path = getFilesDir().getAbsolutePath();

Intent intent = null;

switch (arg0.getId()) {

case R.id.startBtn:

intent = new Intent(this, MenuActivity.class); //내가 이동하고자 하는 클래스명을 쓴다.
startActivity(intent);

break;

}

}
}

4. 이동하고자 하는 화면을 menu.xml 작성을 하고 버튼을 만들어 주고 세팅은 위의 strings.xml 에 적어줍니다.

 

<RelativeLayout 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" >

<Button
android:id="@+id/newButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/new_button"
tools:context=".MenuActivity " />

<Button
android:id="@+id/continueButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="161dp"
android:text="@string/continue_button"
tools:context=".MenuActivity " />

</RelativeLayout>

 

 

5. 이동하고자 하는 클래스를 생성 함

 

public class MenuActivity extends Activity implements OnClickListener

{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);


}


public void onClick(View arg0) {

// TODO Auto-generated method stub

// String path = getFilesDir().getAbsolutePath();

Intent intent = null;

switch (arg0.getId()) {

}

 

6. AndroidManifest.xml 에서는 추가한 자바 클래스명을 추가해줍니다.

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sudoku"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.sudoku.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

               
        <activity android:name=".MenuActivity"   
                  android:label="@string/app_name">
        </activity>
    </application>

</manifest>

 

----------------------------------------

지금 각각의 파일들이 매칭되어야 하는 부분들을 같은 색으로 처리했습니다.

 

안드로이드를 처음 해보시는 부분들은 위에 소스처럼 각각의 부분의 명칭이 일치하도록 하셔야 에러가 없이 작동됩니다.

 

자바소스는 import 되는 부분들은 생략햇습니다. 이클립스에서 빨간불이 들어오는 경우에 살짝 갓다대면 import하라고 나오면 선택하시면 됩니다.

 

*** 결과 화면입니다.

 

 

 

시작이라는 버튼이 보여집니다. 그래서 선택을 하면 아래의 결과가 보입니다.

 

 

두 개의 버튼이 나오는 화면입니다. 이 두개를 선택하면 다른 페이지로 이동하도록 또 만들면 되겠죠 ^^

반응형