一、Activity與Activity之間值的傳遞
先創建兩個活動,一個叫MainActivity,另一個叫SecondActivity
然後在activity_main.xml中添加一個Button,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:text="ToSecondActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toSecond"
android:textAllCaps="false"/>
</LinearLayout>
activity_second.xml中添加一個TextView,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/showData"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
然後分別在MainActivity和SecondActivity中初始化對應的控件
MainActivity代碼如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button toSecond;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
toSecond = (Button)findViewById(R.id.toSecond);
toSecond.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.toSecond:
//跳轉邏輯
break;
}
}
}
SecondActivity代碼如下:
public class SecondActivity extends AppCompatActivity {
private TextView showData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initView();
initData();
}
private void initView() {
showData = (TextView)findViewById(R.id.showData);
}
private void initData(){
//TODo
}
}
第一種:通過setClass方法來實現
在MainActivity的click點擊中添加如下代碼:
case R.id.toSecond:
//跳轉邏輯
Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("name","一枚小垃圾");
intent.putExtra("age","21");
startActivity(intent);
break;
在SecondActivity的
initData()方法中添加如下代碼:
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String age = intent.getStringExtra("age");
showData.setText("姓名:" + name + ";" + "年齡:" + age);
然後運行,我們看下效果:
第二種:通過Bundle來實現:
修改MainActiviy中的case事件代碼爲:
case R.id.toSecond:
//跳轉邏輯
Intent intent = new Intent(this,SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name","一枚小垃圾");
bundle.putString("age","21");
intent.putExtra("bundle",bundle);
startActivity(intent);
break;
然後再將SecondActivity中initData()方法中的代碼修改爲:
private void initData() {
Intent intent = getIntent();
Bundle bundleExtra = intent.getBundleExtra("bundle");
String name = bundleExtra.getString("name");
String age = bundleExtra.getString("age");
showData.setText("姓名:" + name + ";" + "年齡:" + age);
}
下面我們再來看運行效果:
我們可以看到運行效果是一樣。那麼有人要問了,intent和bundle傳值有什麼區別呢。這裏就不贅述了,有興趣的百度一下就行了。
二、Activity與Fragment之間值的傳遞
新建一個FirstFragment類,繼承於Fragment.並重寫onCreateView方法
再新建一個fragment_firset.xml佈局文件。
直接上代碼。
用Bundle傳值
MainActivity完整代碼:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button toSecond;
private FragmentManager fm;
private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
toSecond = (Button) findViewById(R.id.toSecond);
toSecond.setOnClickListener(this);
}
@Override
public void onClick(View v) {
fm = getSupportFragmentManager();
transaction = fm.beginTransaction();
switch (v.getId()) {
case R.id.toSecond:
//跳轉邏輯
FirstFragment firstFragment = new FirstFragment();
Bundle bundle = new Bundle();
bundle.putString("name", "一枚小垃圾");
bundle.putString("age", "22");
firstFragment.setArguments(bundle);
transaction.add(R.id.fragment, firstFragment);
transaction.addToBackStack(null);
break;
}
transaction.commit();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:text="ToSecondActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toSecond"
android:textAllCaps="false"/>
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="300dp"></FrameLayout>
</LinearLayout>
FirstFragment代碼:
public class FirstFragment extends Fragment {
private View view;
private TextView fragment_showData;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_first, container, false);
initView();
initData();
return view;
}
private void initData() {
Bundle bundle = getArguments();
String name = bundle.getString("name");
String age = bundle.getString("age");
fragment_showData.setText("name:" + name + ";"+ "age:" + age +"歲");
}
private void initView() {
fragment_showData = (TextView) view.findViewById(R.id.fragment_showData);
}
}
fragment_first.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_showData"/>
</LinearLayout>
效果:
二、Fragment與Fragment之間值的傳遞
fragment與fragment之間傳值,總共分爲Bundle傳值以及通過接口回調之間的傳值,由於博文篇幅太長了,請移步我的這篇博文。