上一遍是回传数据,当热Activity之间是可以利用Intent很方便地传递很多数据的。
本章就两个Activity之间传递String和Int数据。
1 主要界面:
2 点击按钮之后,就进入第二个activity
这个时候会利用Toast机制显示第一个activity传递什么String和Int给第二个activity。
3 然后点击“return to main activity”,回到第一个activity
同样会显示回传的信息
这样可以在activity之间来回传递需要的信息。
新建项目;
1 首先设计主界面,代码:
<LinearLayout 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" tools:context="com.example.intenttest.MainActivity" > <Button android:id="@+id/seconde_activity_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/activate_secondary" android:onClick="onClick" /> </LinearLayout>
就是构造一个LinearLayout,hold住一个Button按钮,android:onClick="onClick"表示这个按键响应的函数名为"onClick",在逻辑代码中定义好这个函数就可以了,如果没有定义这个函数,那么程序就会崩溃的。
2 构建第二个activity的界面,代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/txt_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/secondary_activity" /> <Button android:id="@+id/btn_OK" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="@string/return_to_main_activity" /> </LinearLayout>
构建一个文本和按钮
3 第一个activity的代码:
package com.example.intenttest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { /* * An integer value that identifies an activity you are calling. When an * Activity returns a value, you must have a way to identify it If the * request code is set to -1, then calling it using the * startActivityForResult() method is equivalent to calling it using the * startActivity() method. That is, no result will be returned. */ int requestCode = 1; public static <T> boolean equ(T t1, T t2) { return t1 == t2; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view) { Intent i = new Intent("su.bill.intenttest.SecondaryActivity"); i.putExtra(SecondaryActivity.extraStr, "Give you a string"); i.putExtra(SecondaryActivity.extraInt, 15); Bundle extras = new Bundle(); extras.putString(SecondaryActivity.extraStr2, "String in data"); extras.putInt(SecondaryActivity.extraInt2, 22); i.putExtras(extras); startActivityForResult(i, requestCode); } @Override public void onActivityResult(int reqCode, int resCode, Intent data) { if (equ(reqCode, requestCode) && equ(resCode, RESULT_OK)) { Toast.makeText(this, Integer.toString(data.getIntExtra(SecondaryActivity.extraInt3, 0)), Toast.LENGTH_SHORT).show(); Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show(); } } @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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
和前一章的代码差不多,这里主要学习的函数是:
1) 让Intent带上String数据:
i.putExtra(SecondaryActivity.extraStr, "Give you a string");
2) 让Intent带上Int数据
i.putExtra(SecondaryActivity.extraInt, 15);
3)还可以使用Bundle数据结构,直接带上String和Int数据:
Bundle extras = new Bundle();
extras.putString(SecondaryActivity.extraStr2, "String in data");
extras.putInt(SecondaryActivity.extraInt2, 22);
i.putExtras(extras);
回传调用的函数onActivityResult还是和前一章一样。使用了Toast显示多个回传的数据。
4 然后是第二个activity的逻辑代码:
package com.example.intenttest; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class SecondaryActivity extends Activity { public static String extraStr = "extra_string"; public static String extraInt = "extra_int"; public static String extraStr2 = "extra_string_2"; public static String extraInt2 = "extra_int_2"; public static String extraInt3 = "extra_int_3"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondary_activity); Toast.makeText(this, getIntent().getStringExtra(extraStr), Toast.LENGTH_SHORT).show(); Toast.makeText(this, String.valueOf(getIntent().getIntExtra(extraInt, 0)), Toast.LENGTH_SHORT).show(); Bundle bundle = getIntent().getExtras(); Toast.makeText(this, bundle.getString(extraStr2), Toast.LENGTH_SHORT) .show(); Toast.makeText(this, Integer.toString(bundle.getInt(extraInt2)), Toast.LENGTH_SHORT).show(); } public void onClick(View v) { Intent i = new Intent(); i.putExtra(extraInt3, 28); i.setData(Uri.parse("pass integer back to main activity")); setResult(RESULT_OK, i); finish(); } }
主要学习代码:
1)取得Intent中的String数据
getIntent().getStringExtra(extraStr)
2)取得Intent中的Int数据:
getIntent().getIntExtra(extraInt, 0)
3)回传String数据:
i.setData(Uri.parse("pass integer back to main activity"));
4)回传函数:
setResult(RESULT_OK, i);
也是利用Toast数据显示。