Android--Activity在跳转时携带数据
Posted chaoyu168
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android--Activity在跳转时携带数据相关的知识,希望对你有一定的参考价值。
首先看看两种传递方法示例:(一个简单姻缘计算器)
主Activity
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); } public void click(View v){ Intent intent = new Intent(this, SecondActivity.class); //把数据封装至intent对象中 // intent.putExtra("malename", "张三"); // intent.putExtra("femalename", "芙蓉姐姐"); //把数据封装至bundle对象中 Bundle bundle = new Bundle(); bundle.putString("malename", "<span style="font-family: Arial, Helvetica, sans-serif;">张三</span>"<span style="font-family: Arial, Helvetica, sans-serif;">);</span> bundle.putString("femalename", "芙蓉姐姐"); //把bundle对象封装至intent对象中 intent.putExtras(bundle); startActivity(intent); } }
import java.util.Random; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent intent = getIntent(); //从intent对象中把封装好的数据取出来 // String maleName = intent.getStringExtra("malename"); // String feMaleName = intent.getStringExtra("femalename"); Bundle bundle = intent.getExtras(); String maleName = bundle.getString("malename"); String feMaleName = bundle.getString("femalename"); Random rd = new Random(); int yinyuan = rd.nextInt(100); TextView tv = (TextView) findViewById(R.id.tv); tv.setText(maleName + "和" + feMaleName + "的姻缘值为" + yinyuan); } }
activity_main.xml:
<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" 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" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是姻缘计算器,很准的哟" /> <EditText android:id="@+id/et_malename" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="张三" /> <EditText android:id="@+id/et_femalename" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="芙蓉姐姐" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计算" android:onClick="click" /> </LinearLayout>
activity_second.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" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是第二个Activity" /> </RelativeLayout>
以上是关于Android--Activity在跳转时携带数据的主要内容,如果未能解决你的问题,请参考以下文章