明示的なIntentによる別のActivityの起動

2598 ワード

手順:
1、新しいクラス
package com.example.atestdemo;

import android.app.Activity;
import android.content.ComponentName;
import android.os.Bundle;
import android.widget.EditText;

public class HelloWorld extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.helloworld);
		
		EditText show = (EditText)findViewById(R.id.show);
		ComponentName comp = getIntent().getComponent();
		//    ComponentName     、  
		show.setText("     :" + comp.getPackageName()
			+ "
:" + comp.getClassName()); } }

2、該当するlayoutを新規作成する
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</LinearLayout>

3、manifestに追加する:
<activity android:name=".HelloWorld"
				android:label="   Activity">
		</activity>
、主関数に次のコードを追加します.
package com.example.atestdemo;

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


public class MainActivity extends Activity {
	
	private EditText latitude = null;
	private EditText longtitude = null;
	 @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		latitude = (EditText)findViewById(R.id.lat);
		longtitude = (EditText)findViewById(R.id.lon);
		Button button = (Button)findViewById(R.id.map);
		
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String _lat = latitude.getText().toString();
				String _lon = longtitude.getText().toString();
				
				ComponentName comp = new ComponentName(MainActivity.this, HelloWorld.class);
				Intent intent = new Intent();
				intent.setComponent(comp);
				startActivity(intent);
			}
		});
	
	}
}