Android中不同的Activity之间的传值方式(Main为当前Activity,Login为目标Activity)
1、使用Intent
Intent intent = new Intent(Main.this, Login.class); intent.putExtra("loginName", "rubekid");
2、使用 静态变量
Person person = new Person(); person.setName("rubekid"); person.setAge(13); Login.person = person;
在Login里面定义静态变量
public static Person person;
3、剪切板(Clipboard)
Main中
ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); clipboardManager.setPrimaryClip(ClipData.newPlainText("loginName", "rubekid"));
Login中
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); clipboardManager.getPrimaryClip().getItemAt(0).getText();
4、使用Application
public class MyApp extends Application{ private 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("测试"); } }
Main中:
myApp = (MyApp)getApplication(); myApp.setName("rubekid");
Login中:
MyApp myApp = (MyApp) getApplication(); loginName.setText(myApp.getName());
时间: 2024-10-13 18:00:51