intentで2つのactivityを切り替えます

4768 ワード


//intent_activity_main.java
package android.demo.intent_activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class intent_activity_main extends Activity {
    /** Called when the activity is first created. */
	Button mbutton;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mbutton=(Button)findViewById(R.id.Button01);

        mbutton.setOnClickListener(A);
    }
    
    public OnClickListener A = new OnClickListener()
    {
		@Override
		public void onClick(View v) 
		{
			// TODO Auto-generated method stub
			Intent intent=new Intent();  
			intent.setClass(intent_activity_main.this, intent_activity_second.class);  
			
			startActivity(intent);
			
			intent_activity_main.this.finish();
		}

     };
}


//intent_activity_second.java
/**
 * 
 */
package android.demo.intent_activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * @author luckfox
 *
 */
public class intent_activity_second extends Activity 
{
	Button mbutton;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        
        mbutton=(Button)findViewById(R.id.Button02);
        mbutton.setOnClickListener(A);
    }
    public OnClickListener A = new OnClickListener()
    {
		@Override
		public void onClick(View v) 
		{
			// TODO Auto-generated method stub
			Intent intent=new Intent();  
			intent.setClass(intent_activity_second.this, intent_activity_main.class);  
			
			startActivity(intent);
			
			intent_activity_second.this.finish();
		}

     };
}


//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="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>


//main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<Button android:text="@+id/Button02" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>


//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="android.demo.intent_activity"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".intent_activity_main"
                  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="intent_activity_second"></activity>
</application>


</manifest>