Android静态传参数(五)

来源:互联网 发布:rds mysql 慢 编辑:程序博客网 时间:2024/06/14 14:20

一、新建OtherActivity.java

public class OtherActivity extends Activity {<span style="color:#ff0000;">public static String name;public static int age;</span>private TextView textview;protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.other);                textview = (TextView)this.findViewById(R.id.msg);        textview.setText("name:"+name+",age:"+age);    }}

定义静态变量name,age.


二、在MainActivity.java,跳转页面的时候调用class OtherActivity的静态变量name、age参数传入

public class MainActivity extends Activity {Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button)this.findViewById(R.id.button1);        button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setClass(MainActivity.this,OtherActivity.class);<span style="color:#ff0000;">OtherActivity.age = 23;OtherActivity.name ="deng";</span>startActivity(intent);}});    }}


三、附上xml

1)activity_main.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" >    <Button        android:id="@+id/button1"        android:layout_height="wrap_content"        android:layout_width="match_parent"        android:text="静态变量传参数测试"      /></RelativeLayout>


2)other.xml

<?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/msg"    android:layout_width="match_parent"    android:layout_height="wrap_content">    </TextView></LinearLayout>
</pre><pre code_snippet_id="620199" snippet_file_name="blog_20150315_6_982152" name="code" class="html">3)AndroidManifest.xml
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.intent_static"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.intent_static.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name=".OtherActivity"            />    </application></manifest>






0 0
原创粉丝点击