Android开发之Intent的传值--Application

时间:2023-03-09 06:46:52
Android开发之Intent的传值--Application

每当我们想要将输入的值传递到多个界面时,只是使用Intent传值的话,就会有一些的弊端。

下面我就以三个页面为例,进行简单的说明一下:

思路:

1.第一个页面是客户输入相关的信息。

2.将客户输入的信息的第一项(我这里设的是name),在第二个页面中进行显示。

3.在第二个页面中直接跳转到第三个页面中,显示客户输入的全部的信息。

首先,在工程中创建一个MyApplication类,类的创建如下:

package com.example.test;



import android.app.Application;



public class MyApplication extends Application{



private static MyApplication singleton;

private String id;

private String name;

private String age;

private String address;

private String email;





public static MyApplication getInstance(){

return singleton;

}



public String getId() {

return id;

}



public void setId(String id) {

this.id = id;

}



public String getName() {

return name;

}



public void setName(String name) {

this.name = name;

}



public String getAge() {

return age;

}



public void setAge(String age) {

this.age = age;

}



public String getAddress() {

return address;

}



public void setAddress(String address) {

this.address = address;

}



public String getEmail() {

return email;

}



public void setEmail(String email) {

this.email = email;

}



public void onCreate(){

super.onCreate();

singleton = this;

}



}

上面创建一个用户的name,age,address,email信息。

在创建Application类时需要注意的是,这个类必须得在配置文件中进行配置才可以,要不然会抛出空异常错误。

具体实现的代码如下:

<application

android:allowBackup="true"

android:name="com.example.test.MyApplication"  //特别注意

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.example.test.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="com.example.test.Activitytwo">

<intent-filter >

<action android:name="android.intent.action.VIEW"/>

</intent-filter>

</activity>

<activity

android:name="com.example.test.Activitythree">

<intent-filter >

<action android:name="android.intent.action.VIEW"/>

</intent-filter>

</activity>

</application>

接下来就是Activity中的代码的实现。在这里我就不写布局文件中的代码了,附上图,大家自己去写,也不是很难。

Android开发之Intent的传值--ApplicationAndroid开发之Intent的传值--Application

Android开发之Intent的传值--Application

MainActivity的代码如下:

package com.example.test;



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.EditText;



public class MainActivity extends Activity {



private MyApplication myapp;

private EditText edtname;

private EditText edtage;

private EditText edtaddress;

private EditText edtemail;

private Button post;

private Button get;

private Button next;

private Button three;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

myapp = MyApplication.getInstance();

edtname = (EditText)findViewById(R.id.name);

edtage = (EditText)findViewById(R.id.age);

edtaddress = (EditText)findViewById(R.id.place);

edtemail = (EditText)findViewById(R.id.email);

next =(Button)findViewById(R.id.next);

next.setOnClickListener(new OnClickListener() {

 

@Override

public void onClick(View v) {

test();

Intent inten = new Intent(MainActivity.this,Activitytwo.class);

startActivity(inten);



}

});

public void test(){



myapp.setName(edtname.getText().toString());

myapp.setAge(edtage.getText().toString());

myapp.setAddress(edtaddress.getText().toString());

myapp.setEmail(edtemail.getText().toString());



}

}

Activitytwo代码实现:

package com.example.test;





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 Activitytwo extends Activity{



private String showname;

private TextView show;

private Button send;



@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activitytwo);



show = (TextView)findViewById(R.id.show);

send = (Button)findViewById(R.id.send);



showname = MyApplication.getInstance().getName();

show.setText(showname);



send.setOnClickListener(new OnClickListener() {



@Override

public void onClick(View arg0) {





Intent intent = new Intent(Activitytwo.this,Activitythree.class);

startActivity(intent);

finish();

}

});



}



}

Activitythree代码实现:

package com.example.test;





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 Activitythree extends Activity{



private TextView showname;

private TextView showage;

private TextView showplace;

private TextView showemail;

private Button finish;



private String name;

private String age;

private String place;

private String email;



@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activitythree);



showname = (TextView)findViewById(R.id.showname);

showage = (TextView)findViewById(R.id.showage);

showplace = (TextView)findViewById(R.id.showplace);

showemail = (TextView)findViewById(R.id.showemail);

finish = (Button)findViewById(R.id.finish);



name = MyApplication.getInstance().getName();

age = MyApplication.getInstance().getAge();

place = MyApplication.getInstance().getAddress();

email = MyApplication.getInstance().getEmail();



showname.setText(name);

showage.setText(age);

showplace.setText(place);

showemail.setText(email);



finish.setOnClickListener(new OnClickListener() {



@Override

public void onClick(View v) {

Intent intent = new Intent(Activitythree.this,MainActivity.class);

startActivity(intent);

finish();

}

});



}

}

上述就是我的基本的代码的实现以及界面的设计,界面不美观只是为了实现功能而已。

Application类是实现多个Activity之间共享数据。

希望上面的说明能够让大家明白。