Androi Activity实验1 Intent(意图)

来源:互联网 发布:莫言丑化中国知乎 编辑:程序博客网 时间:2024/06/03 21:35

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;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);button = (Button) this.findViewById(R.id.myButton);// 给按钮注册监听事件, 参数是新建的myButtonListenerbutton.setOnClickListener(new myButtonListener());}// 内部类, 实现OnClickListener接口 Intent在此声明并调用class myButtonListener implements OnClickListener{@Overridepublic 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;@Overrideprotected 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{@Overridepublic 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> 


 

六、运行程序

如何将项目安装在模拟器中之前文章说过了,这里不做累述。

 

 

原创粉丝点击