안드로이드 학습노트--buttonactivity

프로젝트의 기능은 하나의acticity에서 단추를 누르면 다른activity로 전환하는 것입니다
 
다음 코드는 MainActivity입니다.java의 코드
package com.example.button_activity;



import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;



public class MainActivity extends Activity {



    private Button myButton = null; 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myButton = (Button)findViewById(R.id.myButton);

        myButton.setOnClickListener(new MyButtonListener());  

    }

    

    class MyButtonListener implements OnClickListener{



        @Override

        public void onClick(View v) {

            // TODO Auto-generated method stub

        Intent intent = new Intent();

        intent.setClass(MainActivity.this, OtherActivity.class);

        MainActivity.this.startActivity(intent);

        }

        

        

    }

        

}

 
다음 코드는 Otheractivity입니다.java의 코드
package com.example.button_activity;



import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;



public class OtherActivity extends Activity{



    private TextView myTextView = null; 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_other);

        myTextView = (TextView)findViewById(R.id.myTextView);

        myTextView.setText(R.string.other);

    }

    



}

 
다음 코드는 activitymain.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"

    tools:context="${relativePackage}.${activityClass}" >



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />

    

    <Button

        android:id="@+id/myButton"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        

        />



</RelativeLayout>

 
다음 코드는 activityother.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"

    tools:context="${relativePackage}.${activityClass}" >



    <TextView

        android:id="@+id/myTextView"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />

    



</RelativeLayout>

 
다음 코드는string입니다.xml 코드
<?xml version="1.0" encoding="utf-8"?>

<resources>



    <string name="app_name">button_activity</string>

    <string name="hello_world">Hello world!</string>

    <string name="other">OtherActivity</string>

</resources>

 
다음 코드는 AndroidManifest입니다.xml 코드
패키지 이름 수정
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.button_activity"

    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"

        android:theme="@style/AppTheme" >

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

            </intent-filter>

        </activity>

        <activity

            android:name=".OtherActivity"

            android:label="@string/other" >

        </activity>     

    </application>



</manifest>

 
실행할 수 없으면 kill-adb와 start-adb를 실행하고 eclipse를 다시 시작하십시오

좋은 웹페이지 즐겨찾기