【Android(安卓)开发教程】使用Intent传递数据
16lz
2021-01-25
本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/除了能从一个Activity返回数据结果之外,向一个Activity传递数据也是很常用的。
1. 新建一个工程,PassData。
2. main.xml中的代码。
[java] view plain copy- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <Button
- android:id="@+id/btn_SecondActivity"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="onClick"
- android:text="ClicktogotoSecondActivity"/>
- </LinearLayout>
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="WelcometoSecondActivity"/>
- <Button
- android:id="@+id/btn_MainActivity"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="onClick"
- android:text="Clicktoreturntomainactivity"/>
- </LinearLayout>
- publicclassSecondActivityextendsActivity{
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.secondactivity);
- //---getthedatapassedinusinggetStringExtra()---
- Toast.makeText(this,getIntent().getStringExtra("str1"),
- Toast.LENGTH_SHORT).show();
- //---getthedatapassedinusinggetIntExtra()---
- Toast.makeText(this,
- Integer.toString(getIntent().getIntExtra("age1",0)),
- Toast.LENGTH_SHORT).show();
- //---gettheBundleobjectpassedin---
- Bundlebundle=getIntent().getExtras();
- //---getthedatausingthegetString()---
- Toast.makeText(this,bundle.getString("str2"),Toast.LENGTH_SHORT)
- .show();
- //---getthedatausingthegetInt()method---
- Toast.makeText(this,Integer.toString(bundle.getInt("age2")),
- Toast.LENGTH_SHORT).show();
- }
- publicvoidonClick(Viewview){
- //---useanIntentobjecttoreturndata---
- Intenti=newIntent();
- //---usetheputExtra()methodtoreturnsome
- //value---
- i.putExtra("age3",45);
- //---usethesetData()methodtoreturnsomevalue---
- i.setData(Uri.parse("Somethingpassedbacktomainactivity"));
- //---settheresultwithOKandtheIntentobject---
- setResult(RESULT_OK,i);
- //---destroythecurrentactivity---
- finish();
- }
- }
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="net.horsttnann.PassingData"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdkandroid:minSdkVersion="10"/>
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name">
- <activity
- android:name=".PassingDataActivity"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- <activity
- android:name="net.manoel.PassingData.SecondActivity"
- android:label="SecondActivity">
- <intent-filter>
- <actionandroid:name="net.horsttnann.PassingDataSecondActivity"/>
- <categoryandroid:name="android.intent.category.DEFAULT"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>
6. PassDataActivity中的代码。
public class PassingDataActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View view) { Intent i = new Intent("net.manoel.PassingDataSecondActivity"); //---use putExtra() to add new key/value pairs--- i.putExtra("str1", "This is a string"); i.putExtra("age1", 25); //---use a Bundle object to add new key/values // pairs--- Bundle extras = new Bundle(); extras.putString("str2", "This is another string"); extras.putInt("age2", 35); //---attach the Bundle object to the Intent object--- i.putExtras(extras); //---start the activity to get a result back--- startActivityForResult(i, 1); } public void onActivityResult(int requestCode, int resultCode, Intent data) { //---check if the request code is 1--- if (requestCode == 1) { //---if the result is OK--- if (resultCode == RESULT_OK) { //---get the result using getIntExtra()--- Toast.makeText(this, Integer.toString( data.getIntExtra("age3", 0)), Toast.LENGTH_SHORT).show(); //---get the result using getData()--- Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show(); } } }}
7. 按F11调试。
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
- Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- Android(安卓)用纯代码实现复杂界面
- android将path拆分为多个path
- android关于AlertDialog加入EditText无法弹出键盘的问题
- android 数据库 sqlite数据类型
- Android读写文件 (2011-05-15 11:22:22)