简述:
刚开始写Android程序, 需要对其代码写法有一定了解, 通过查看资料,觉得Intent这个对象在里面起到很大的作用,所以想通过熟悉这个对象,进而Android入门
实验设计:
实现button点击之后的Activity(页面跳转)
从MainActivity, 跳转到Activity1, 同时传递mainPageInfo值
步骤:
新建一个Android项目, 名为AndroidTestProject
创建主页面为MainActivity,
1)在它的onCreate函数里加入一个button, 名为 toActivity1Btn
但是在这一步之前, 需要写到main_acitvity.xml中添加这个button
main_activity.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=".MainActivity" >
<Button
android:id="@+id/toActivity1_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="17dp"
android:text="@string/toActivity1_btn" />
</RelativeLayout>
2)这是发现最后一行
android:text="@string/toActivity1_btn" />
这么写是为了方便统一管理命名方式, 所以还需要到string.xml中注册这个toActivity1_btn的名字
string.xml(后面还要修改这个文件)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidTestProject</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="toActivity1_btn">Jump To Activity1</string>
</resources>
于是添加了上面string.xml文件中的toActivity1_btn的命名
3) 在MainActivity.java中实际调用toActivity1_btn, 并且加入页面跳转的click事件
package atp.ui;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button toActivity1_btn = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
toActivity1_btn = (Button)findViewById(R.id.toActivity1_btn);
toActivity1_btn.setOnClickListener(new toActivity1BtnClickListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity, menu);
return true;
}
private class toActivity1BtnClickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
//create intent
Intent intent = new Intent();
intent.setClass(MainActivity.this, Activity1.class);
//create a bundle, then put it into intent
Bundle bundle = new Bundle();
bundle.putString("mainPageInfo", "Hello Activity1"); // 压入数据
intent.putExtras(bundle);
//use intent to define the page direction
MainActivity.this.startActivity(intent);
}
}
}
4)创建Activity1, 用来作为上面三个步骤的跳转页面
同时在activity1.xml(即Activity1.java所对应的页面文件)加入一个textView用来输出刚才从MainActivity中传来的mainPageInfo的值
activity1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView_activity1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/textView_activity1" >
</TextView>
</LinearLayout>
Activity1.java
package atp.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class Activity1 extends Activity {
private TextView textView_activity1 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
Bundle bundleFromMainActivity = this.getIntent().getExtras();
textView_activity1 = (TextView)findViewById(R.id.textView_activity1);
String mainPageInfo = bundleFromMainActivity.getString("mainPageInfo");
textView_activity1.append(mainPageInfo);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity1, menu);
return true;
}
}
strings.xml 中再加入textView的string值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidTestProject</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="toActivity1_btn">Jump To Activity1</string>
<string name="title_activity1">Activity1</string>
<string name="textView_activity1">Show Info From MainActivity: </string>
</resources>
5) 千万不能遗忘的在AndroidManifest.xml 中需要注册这个activity, 路径很重要
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="atp.ui"
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=".Activity1"
android:label="@string/title_activity1" >
</activity>
</application>
</manifest>
完成之后的效果图:
MainActivity
Activity1