Activity

来源:互联网 发布:淘宝城三期商业中心 编辑:程序博客网 时间:2024/04/29 05:11
package com.example.activity2;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.TextView;public class MainActivity extends Activity {int page=0;TextView tv_show;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);page = 40;if(savedInstanceState!=null){System.out.println("当前页为:"+savedInstanceState.getInt("page"));}tv_show = (TextView) this.findViewById(R.id.tv_show);}public void on(View view){switch (view.getId()) {case R.id.btn_1:Intent intent = new Intent();//intent.setClass(this, OtherActivity.class);//intent.setClassName("com.example.activity2", "com.example.activity2.OtherActivity");//intent.setAction("android.intent.action.CALL");//intent.setData(Uri.parse("tel://10086"));//intent.setAction("android.intent.action.VIEW");//intent.setData(Uri.parse("http://www.baidu.com"));//intent.setAction("android.intent.action.VIEW");//intent.setDataAndType(Uri.parse("file:///mnt/sdcard/1.mp3"),"audio/*");//intent.setAction("android.intent.action.VIEW");//intent.setDataAndType(Uri.parse("file:///mnt/sdcard/1.jpg"),"image/*");/*Bundle bundle1 = new Bundle();bundle1.putString("name", "aa");bundle1.putInt("age", 1);Bundle bundle2 = new Bundle();bundle2.putString("name", "bb");bundle2.putInt("age", 2);intent.setClass(this, OtherActivity.class);intent.putExtra("bundle1",bundle1);intent.putExtra("bundle2",bundle2);*/Person p1 = new Person("aa",11);Person p2 = new Person("bb",22);intent.setClass(this,OtherActivity.class);intent.putExtra("person1", p1);intent.putExtra("person2", p2);Person p3 = new Person("cc",33);intent.setClass(this, OtherActivity.class);intent.putExtra("person1", p3);startActivityForResult(intent, 100);break;default:break;}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if(requestCode==100 && resultCode==100){Person p1 = (Person) data.getSerializableExtra("person3");tv_show.setText(p1.getName()+":"+p1.getAge());}}@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);outState.putInt("page", page);}}


package com.example.activity2;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class OtherActivity extends Activity {Button btn_close;TextView tv_show;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_other);tv_show = (TextView) this.findViewById(R.id.othershow);/*Intent intent = getIntent();Bundle data1 = intent.getBundleExtra("bundle1");String name1 = data1.getString("name");int age1 = data1.getInt("age");Bundle data2 = intent.getBundleExtra("bundle2");String name2 = data2.getString("name");int age2 = data2.getInt("age");tv_show.setText(name1+":"+age1+";"+name2+":"+age2);*/Intent intent = getIntent();Person p1 = (Person) intent.getSerializableExtra("person1");String name1 = p1.getName();int age1 = p1.getAge();Person p2 = (Person) intent.getSerializableExtra("person2");String name2 = p2.getName();int age2 = p2.getAge();tv_show.setText(name1+":"+age1+";"+name2+":"+age2);}public void on(View view){switch (view.getId()) {case R.id.otherclose:Intent data = new Intent();Person p3 = new Person("dd",44);data.putExtra("person3", p3);setResult(100, data);OtherActivity.this.finish();break;default:break;}}}

package com.example.activity2;import java.io.Serializable;public class Person implements Serializable{private String name;private int age;public Person(){}public Person(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.activity2"    android:versionCode="1"    android:versionName="1.0" >    <uses-permission android:name="android.permission.CALL_PHONE" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name="com.example.activity2.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />                <action android:name="android.intent.action.CALL" />                <action android:name="android.intent.action.VIEW" />                <data android:scheme="tel" />                <data android:scheme="http" />                <data android:scheme="file" android:mimeType="audio/*" />                <data android:scheme="file" android:mimeType="video/*" />                <data android:scheme="file" android:mimeType="image/*" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>        <activity            android:name="com.example.activity2.OtherActivity"            android:label="@string/title_activity_other" >        </activity>    </application></manifest>


0 0
原创粉丝点击