安卓-----開啟新的Activity,並傳遞若干個引數給它
阿新 • • 發佈:2019-01-09
第一種傳遞引數:
1、佈局檔案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/btn_go" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="169dp" android:onClick="go" android:text="@string/btn_go" /> </RelativeLayout>
activity_other.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/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:onClick="back" android:text="@string/btn_back" /> </RelativeLayout>
2、MainActivity
package com.example.lession12_activity; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @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; } public void go(View v){ //攜帶資料 Intent intent=new Intent(MainActivity.this, OtherActivity.class); Bundle bundle=new Bundle(); bundle.putString("name", "shuang"); bundle.putString("pass", "123"); //把附加的資料放到意圖當中 intent.putExtras(bundle); //執行意圖 startActivity(intent); } }
OtherActivity獲取引數的值:
package com.example.lession12_activity;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class OtherActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設定顯示的檢視
setContentView(R.layout.activity_other);
//獲取資料
Bundle bundle=this.getIntent().getExtras();
Toast.makeText(this, "姓名:"+bundle.get("name")+"密碼"+bundle.get("pass"), Toast.LENGTH_LONG).show();
}
public void back(View v){
Intent intent=new Intent();
//intent.setClassName("com.example.lession12_activity", "com.example.lession12_activity.MainActivity");
ComponentName component=new ComponentName("com.example.lession12_activity", "com.example.lession12_activity.MainActivity");
intent.setComponent(component);
startActivity(intent);
}
}
第二種傳遞引數的方法(就是在MainActivity中存值和OtherActivity中獲取值的方法需要改變,其他的不變):
package com.example.lession12_activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
public void go(View v){
//建立意圖物件
/*Intent intent=new Intent();
intent.setClass(this, OtherActivity.class);*/
//執行意圖
//startActivity(intent);
//自定義隱士意圖
/*Intent intent=new Intent();
//自定義的action
intent.setAction("www.csdn.net.hbsi");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.parse("csdn://www.csdn.net/xx"),"image/jpeg");
//執行意圖
startActivity(intent);*/
//攜帶資料
Intent intent=new Intent(MainActivity.this, OtherActivity.class);
/*Bundle bundle=new Bundle();
bundle.putString("name", "shuang");
bundle.putString("pass", "123");*/
//把附加的資料放到意圖當中
//intent.putExtras(bundle);
intent.putExtra("name", "shuang");
intent.putExtra("pass", "123");
//執行意圖
startActivity(intent);
}
}
OtherActivity:
package com.example.lession12_activity;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class OtherActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設定顯示的檢視
setContentView(R.layout.activity_other);
//獲取資料
Bundle bundle=this.getIntent().getExtras();
Intent intent=this.getIntent();
Toast.makeText(this, "姓名:"+intent.getStringExtra("name")+"密碼:"+intent.getStringExtra("pass"), Toast.LENGTH_LONG).show();
//Toast.makeText(this, "姓名:"+bundle.get("name")+"密碼"+bundle.get("pass"), Toast.LENGTH_LONG).show();
}
public void back(View v){
Intent intent=new Intent();
//intent.setClassName("com.example.lession12_activity", "com.example.lession12_activity.MainActivity");
ComponentName component=new ComponentName("com.example.lession12_activity", "com.example.lession12_activity.MainActivity");
intent.setComponent(component);
startActivity(intent);
}
}
記得在專案清單中配置OtherActivity!