Activity之間參數傳遞

1、第一種,簡單傳遞。

發送:

switch (v.getId()) {

case R.id.btn1:

EditText editText = findViewById(R.id.editText);

Intent intent = new Intent(this, my1Activity.class);

intent.putExtra("data", editText.getText().toString());

startActivity(intent);

break;

接收:

Intent intent=getIntent();

String str1 = intent.getStringExtra("data");

TextView textView=findViewById(R.id.textView3);

textView.setText(str1);

2、第二種:使用bundle.外匯常見問題
http://www.kaifx.cn/lists/question/

發送:

Bundle bundle=new Bundle();

bundle.putString("data1",editText.getText().toString()+editText.getText().toString());

intent.putExtras(bundle);

startActivity(intent);

接收:

Bundle bundle=getIntent().getExtras();

textView1.setText(bundle.getString("data1").toString());

3、相對複雜:

發送端:

case R.id.btn1:

EditText editText = findViewById(R.id.editText);

Intent intent = new Intent(this, my1Activity.class);

//定義。

Mapmap=new HashMap<>();

map.put("key1","Value1");

map.put("key2","Value1");

List> list=new ArrayList<>();

list.add(map);

//須定義一個list用於在budnle中傳遞需要傳遞的

ArrayList<Object>,這個是必須要的!
Bundle bundle=new Bundle();
ArrayList bundlelist=new ArrayList();
bundlelist.add(list);
bundle.putParcelableArrayList("list",bundlelist);
intent.putExtras(bundle);
startActivity(intent);
break;

接收:

//接收參數
Bundle bundle=getIntent().getExtras();
ArrayList list =bundle.getIntegerArrayList("list");
//把list中的參數,轉換會
List<Map<String,Object>> lists=( List<Map<String,Object>>)list.get(0);

String sResult = "";
for (Map<String, Object> m : lists) {
for (String k : m.keySet()) {
sResult += "\r\n" + k + " : " + m.get(k);
}
}

TextView textView=findViewById(R.id.textView3);
TextView textView1=findViewById(R.id.textView4);
textView1.setText(sResult);

4、定義全局變量:
定義:

public class my_data{
public static String m1;
public static Integer d1;
}
使用:


textView.setText(my_data.m1);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章