这次讲解一下如何使用静态变量来传递数据,
原理其实很简单,就是在接收端的Avtivity里面设置static的变量,在发送端这边改变静态变量的值,然后启动意图。
效果图为:
发送端截图:
接收端截图:
上代码:
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button)findViewById(R.id.btOpenOtherActivity); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //定义一个意图 Intent intent = new Intent(MainActivity.this,OtherActivity.class); //改变OtherActivity的三个静态变量的值 OtherActivity.name = "wulianghuan"; OtherActivity.age = "22"; OtherActivity.address = "上海闵行"; startActivity(intent); } }); } }
OtherActivity.java
import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import com.example.hello10.R; public class OtherActivity extends Activity { public static String name; public static String age; public static String address; private TextView text_name; private TextView text_age; private TextView text_address; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); text_name = (TextView) findViewById(R.id.name); text_age = (TextView) findViewById(R.id.age); text_address = (TextView) findViewById(R.id.address); //设置文本框的数据 text_name.setText("姓名:"+name); text_age.setText("年龄:"+age); text_address.setText("地址:"+address); } }
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">hello10</string> <string name="action_settings">Settings</string> <string name="tip1">这是:MainActivity</string> <string name="tip2">使用静态变量传递数据</string> <string name="tip3">Hello world!</string> </resources>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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="@string/tip1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btOpenOtherActivity" android:text="@string/tip2"/> </LinearLayout>
other.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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:id="@+id/name" android:text="" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/age" android:text="" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/address" android:text="" /> </LinearLayout>