标签:
Android学习 Day3 Activity几种参数传递方式
昨天已经使用intent跳转并且传递数值
今天使用剩下的三种方式。传递变量。
使用Intent可以很方便在不同的Activity之间传递数据,这个也是官方推荐的方式,但是也有一定的局限性,就是Intent无法传递不能序列化的对象。我们可以使用静态变量来解决这个问题
首先在OtherActivity生命静态全局变量
然后在mainActivity设置。
主要代码OtherActivity:
private TextView msg;
public static String name;
public static String address;
public static int age;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
msg = (TextView)this.findViewById(R.id.msg);
msg.setText("姓名:"+name+"\n"+"年龄:"+age+"\n"+"地址:"+address);
}
mainActivity:
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
OtherActivity.age = 21;
OtherActivity.name = "刘帅康";
OtherActivity.address = "利辛县";
startActivity(intent);
MainActivity.java package com.example.day3intent2; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) this.findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MainActivity.this, OtherActivity.class); OtherActivity.age = 21; OtherActivity.name = "刘帅康"; OtherActivity.address = "利辛县"; startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
otherActivity.java package com.example.day3intent2; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class OtherActivity extends Activity { private TextView msg; public static String name; public static String address; public static int age; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); msg = (TextView)this.findViewById(R.id.msg); msg.setText("姓名:"+name+"\n"+"年龄:"+age+"\n"+"地址:"+address); } }
Activity_main.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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="静态变量传递数值" /> </RelativeLayout> Activity_other.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="default" /> </LinearLayout>
在Activity之间数据传递还可以利用一些技巧,不管是Windows还是Linux操作系统,都会支持一种叫剪切板的技术,也就是某一个程序将一些数据复制到剪切板上,然后其他的任何程序都可以从剪切板中获取数据。
MainActivity.java package com.example.day3intent3; import java.net.ContentHandler; import android.R.integer; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) this.findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @SuppressLint("NewApi") @SuppressWarnings("deprecation") @Override public void onClick(View arg0) { // TODO Auto-generated method stub //获取系统剪贴板服务到fs【发送】 ClipboardManager fs = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); //定义发送内容以及发送 String fscontent = "姓名:刘帅康\n年龄:21\n地址:利辛县"; fs.setText(fscontent);//向剪贴板发送! Intent intent = new Intent(); intent.setClass(MainActivity.this, OtherActivity.class); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
OtherActivity.java package com.example.day3intent3; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ClipboardManager; import android.content.Context; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.widget.TextView; public class OtherActivity extends Activity { private TextView msg; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); msg = (TextView) this.findViewById(R.id.msg); // 定义一个剪贴板接收器js【接受】! ClipboardManager js = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); String message;// 定义存储内容的变量 message = js.getText().toString();// 接收剪贴板文本 msg.setText(message); } }
在Activity之间数据传递中还有一种比较实用的方式,就是全局对象,实用J2EE的读者来说都知道Java Web的四个作用域,这四个作用域从小到大分别是Page、Request、Session和Application,其中Application域在应用程序的任何地方都可以使用和访问,除非是Web服务器停止,Android中的全局对象非常类似于Java Web中的Application域,除非是Android应用程序清除内存,否则全局对象将一直可以访问。
需要新建一个集成application的类!例如类名为MyName 并且在manifest里面需要在application标签里声明android:name=".MyName"
MyName.java package com.example.day4intent4; import android.app.Application; public class MyName extends Application { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); setName("张三"); } }
MainActivity.java package com.example.day4intent4; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button btn1; private MyName myname; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) this.findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub myname = (MyName) getApplication(); myname.setName("刘帅康"); Intent intent = new Intent(MainActivity.this, OtherActivity.class); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
MainActivity.java package com.example.day4intent4; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button btn1; private MyName myname; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) this.findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub myname = (MyName) getApplication(); myname.setName("刘帅康"); Intent intent = new Intent(MainActivity.this, OtherActivity.class); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Application传递数据"/> </RelativeLayout>
Other_activity.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/msg" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="default text"/> </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/ilsk/p/4394198.html