您的位置:首页 > 移动开发 > Android开发

通过显式Intent启动另一个Activity

2016-03-25 18:55 603 查看
步骤:

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()
+ "\n组件类名为:" + 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>
4、在主函数里添加以下代码:

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);
}
});

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android