您好,登錄后才能下訂單哦!
Intent意圖 可用于Activity之間的數(shù)據傳遞,一般可分為下面兩種情況,從當前Activity傳遞到目標Activity后有無返回值:
1.傳遞后無返回值的情況:
在起始Activity中,發(fā)送數(shù)據 protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.thisactivity); Intent intent = new Intent(); //設置起始Activity和目標Activity,表示數(shù)據從這個Activity傳到下個Activity intent.setClass(ThisActivity.this,TargetActivity.class); //綁定數(shù)據 intent.putExtra("username",username);//也可以綁定數(shù)組 intent.putExtra("userpass",userpass); //打開目標Activity startActivity(intent); } 在目標Activity中,接收數(shù)據: protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.targetactivity); //獲得意圖 Intent intent = getIntent(); //讀取數(shù)據 String name = intent.getStringExtra("username"); String pass = intent.getStringExtra("userpass); } 也可以適用Bundle(捆) 在起始Activity中,發(fā)送數(shù)據: protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.thisactivity); Intent intent = new Intent(); //設置起始Activity和目標Activity,表示數(shù)據從這個Activity傳到下個Activity intent.setClass(ThisActivity.this,TargetActivity.class); //一次綁定多個數(shù)據 Bundle bundle = new Bundle(); bundle.putString("username",username); bundle.putString("userpass",userpass); intent.putExtras(bundle); //打開目標Activity startActivity(intent); } 在目標Activity中,接收數(shù)據: protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.targetactivity); //獲得意圖 Intent intent = getIntent(); //讀取數(shù)據 Bundle bundle = intent.getExtras(); String name = bundle.getString("username"); String pass = bundle.getString("userpass"); }
2.傳遞后有返回值的情況:
當需要從目標Activity回傳數(shù)據到原Activity時,可以使用上述方法定義一個新的Intent來傳遞數(shù)據,也可以使用startActivityForResult (Intent intent, int requestCode);方法。
在起始Activity中,發(fā)送數(shù)據: protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.thisactivity); Intent intent = new Intent(); //設置起始Activity和目標Activity,表示數(shù)據從這個Activity傳到下個Activity intent.setClass(ThisActivity.this,TargetActivity.class); //綁定數(shù)據 intent.putExtra("username",username);//也可以綁定數(shù)組 intent.putExtra("userpass",userpass); //打開目標Activity startActivityForResult(intent,1); } //需要重寫onActivityResult方法 protected void onActivityResult(int requestCode, int resultCode, Intent intent){ super.onActivityResult(requestCode,resultCode,intent); //判斷結果碼是否與回傳的結果碼相同 if(resultCode == 1){ //獲取回傳數(shù)據 String name = intent.getStringExtra("name"); String pass = intent.getStringExtra("pass); //對數(shù)據進行操作 ...... } 在目標Activity中,接收數(shù)據: protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.targetactivity); //獲得意圖 Intent intent = getIntent(); //讀取數(shù)據 String name = intent.getStringExtra("username"); String pass = intent.getStringExtra("userpass); //從EditText中獲取新的數(shù)據給name和pass name = editText1.getText().toString(); pass = editText2.getText().toString() //數(shù)據發(fā)生改變,需要把改變后的值傳遞回原來的Activity intent.putExtra("name",name); intent.putExtra("pass",pass); //setResult(int resultCode,Intent intent)方法 setResult(1,intent); //銷毀此Activity,摧毀此Activity后將自動回到上一個Activity finish(); }
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。