1 public class MainActivity extends Activity { 2 private Button btn1=null; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main);//加载布局文件 7 btn1=(Button)findViewById(R.id.button1); 8 btn1.setOnClickListener(new View.OnClickListener() { 9 10 @Override 11 public void onClick(View v) { 12 User user=new User("zhangsan", "bj", 23); 13 //将对象转换成字符串 14 String base64=null; 15 //序列化对象 16 ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(); 17 try { 18 ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream); 19 objectOutputStream.writeObject(user); 20 base64=Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT); 21 objectOutputStream.close(); 22 23 } catch (Exception e) { 24 // TODO: handle exception 25 } 26 27 //数据放入剪切板 28 android.content.ClipboardManager clipboardManager=(android.content.ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); 29 clipboardManager.setText(base64); 30 Intent intent=new Intent(MainActivity.this,SecondActivity.class); 31 startActivity(intent); 32 } 33 34 }); 35 } 36 }
转下个activity
public class SecondActivity extends Activity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.firstr); textView =(TextView)findViewById(R.id.mss); //获取剪切板的数据 ClipboardManager clipboardManager=(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); String message=clipboardManager.getText().toString(); byte[] buffer=Base64.decode(message, Base64.DEFAULT); ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(buffer); try{ ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream); User user=(User)objectInputStream.readObject(); textView.setText(user.toString()); }catch(Exception e){ e.printStackTrace(); } } }
2.通过全局变量传递数据
定义全局变量
1 public class AllApplication extends Application { 2 private String username; 3 4 5 public String getUsername() { 6 return username; 7 } 8 9 10 public void setUsername(String username) { 11 this.username = username; 12 } 13 14 15 @Override 16 public void onCreate() { 17 super.onCreate(); 18 setUsername("zhangsanas"); 19 } 20 }
在清单表中加入全局类名称
1 <application 2 android:name=".AllApplication" 3 android:allowBackup="true" 4 android:icon="@drawable/ic_launcher" 5 android:label="@string/app_name" 6 android:theme="@style/AppTheme" >
主activity
1 public class Main extends Activity { 2 private Button btn1=null; 3 private AllApplication all; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.main); 8 btn1=(Button)findViewById(R.id.btn); 9 btn1.setOnClickListener(new View.OnClickListener() { 10 11 @Override 12 public void onClick(View v) { 13 all=(AllApplication)getApplication(); 14 all.setUsername("wangwu"); 15 Intent intent=new Intent(Main.this,Second.class); 16 startActivity(intent); 17 18 } 19 }); 20 } 21 }
转向下个
1 public class Second extends Activity { 2 private AllApplication allApplication; 3 private TextView textView; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 // TODO Auto-generated method stub 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.second); 9 10 textView=(TextView)findViewById(R.id.mess); 11 allApplication=(AllApplication)getApplication(); 12 textView.setText(allApplication.getUsername()); 13 14 } 15 16 }
带返回参数
1 public class Main extends Activity { 2 private Button btn1=null; 3 //定义一个请求的标志量 4 private static final int REQUEST_CODE=1; 5 private EditText one,two,result; 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.main); 10 one=(EditText)findViewById(R.id.one); 11 two=(EditText)findViewById(R.id.two); 12 result=(EditText)findViewById(R.id.result); 13 14 btn1=(Button)findViewById(R.id.btn); 15 btn1.setOnClickListener(new View.OnClickListener() { 16 17 @Override 18 public void onClick(View v) { 19 Intent intent=new Intent(); 20 intent.setClass(Main.this, Second.class); 21 intent.putExtra("a", one.getText().toString()); 22 intent.putExtra("b", two.getText().toString()); 23 startActivityForResult(intent, REQUEST_CODE); 24 25 } 26 }); 27 } 28 @Override 29 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 30 if(resultCode==2){ 31 if(requestCode==REQUEST_CODE){ 32 result.setText(data.getStringExtra("three")); 33 } 34 } 35 } 36 }
1 public class Second extends Activity { 2 private EditText editText; 3 private TextView teView; 4 private Button bt; 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 // TODO Auto-generated method stub 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.second); 10 bt=(Button)findViewById(R.id.rebtn); 11 teView=(TextView)findViewById(R.id.mess); 12 editText=(EditText)findViewById(R.id.three); 13 Intent intent=getIntent(); 14 String a=intent.getStringExtra("a"); 15 String b=intent.getStringExtra("b"); 16 teView.setText(a+" + "+b+" = ?"); 17 bt.setOnClickListener(new View.OnClickListener() { 18 19 @Override 20 public void onClick(View v) { 21 Intent in=new Intent(); 22 in.putExtra("three",editText.getText().toString()); 23 setResult(2, in); 24 finish(); 25 26 } 27 }); 28 29 30 31 } 32 33 }
时间: 2024-10-28 14:37:55