Androi Activity实验1 Intent(意图)
Androi Activity实验1 Intent(意图)
一、建立Android项目
建立项目前几篇文章已经说过了这里不再累述。
二、编写资源文件(字符文件)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Activity_02!</string>
<string name="app_name">Activity_02</string>
<string name="buttonName">跳转</string>
<string name="textViewValue">跳转后显示</string>
<string name="rebackButtonText">跳回去</string>
</resources>
三、编写资源文件(布局文件)
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"
>
<Button
android:id="@+id/myButton"
android:text="@string/buttonName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
other.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:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/textViewValue"
/>
<Button
android:id="@+id/rebackButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/rebackButtonText"
/>
</LinearLayout>
四、编写核心程序(屏幕)
Activity_02
package com.home.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;
/**
* 通过这个Activity跳转到另一个Activity通过 Intent 传递
*
* @author Administrator
*
*/
public class Activity_02 extends Activity
{
Button button = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) this.findViewById(R.id.myButton);
// 给按钮注册监听事件, 参数是新建的myButtonListener
button.setOnClickListener(new myButtonListener());
}
// 内部类, 实现OnClickListener接口 Intent在此声明并调用
class myButtonListener implements OnClickListener
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
// 通过Intent给Activity传递值
intent.putExtra("value", "这是上一个Activity通过Intent传过来的");
intent.setClass(Activity_02.this, OtherActivity.class);
Activity_02.this.startActivity(intent);// 启动一个Activity
}
}
}
OtherActivity
package com.home.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;
import android.widget.TextView;
public class OtherActivity extends Activity
{
Button button = null;
TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(com.home.activity.R.layout.other);
button = (Button) this
.findViewById(com.home.activity.R.id.rebackButton);
textView = (TextView) this
.findViewById(com.home.activity.R.id.textView);
// 取得上一个Activity通过Intent传过来的数值
Intent intent = getIntent();
String value = intent.getStringExtra("value");
// 把取得的值付给TextView显示
textView.setText(value);
button.setOnClickListener(new RebackButtonListener());
}
// 内部类 用于生命Intent和监听事件
class RebackButtonListener implements OnClickListener
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(OtherActivity.this, Activity_02.class);
OtherActivity.this.startActivity(intent);
}
}
}
五、编写清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.home.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity_02"
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/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
六、运行程序
如何将项目安装在模拟器中之前文章说过了,这里不做累述。