Android靜態傳參數(五)

來源:互聯網
上載者:User

標籤:

一、建立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>






Android靜態傳參數(五)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.