码迷,mamicode.com
首页 > 移动开发 > 详细

Android Activity全局数据共享

时间:2016-06-13 20:36:51      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:

技术分享技术分享

 1 package com.example.metrox.l13;
 2 
 3 import android.app.Application;
 4 
 5 /**
 6  * Created by MetroX on 2016/6/13.
 7  */
 8 public class App extends Application {
 9     private String textDate = "Default";
10 
11     public String getTextDate() {
12         return textDate;
13     }
14 
15     public void setTextDate(String textDate) {
16         this.textDate = textDate;
17     }
18 }
 1 package com.example.metrox.l13;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.EditText;
 7 import android.widget.TextView;
 8 
 9 public class MainActivity extends AppCompatActivity {
10 
11     TextView tv;
12     EditText et ;
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.main1);
17         tv = (TextView) findViewById(R.id.textView);
18         et = (EditText)findViewById(R.id.editText);
19         tv.setText("ShareData: " + ((App)getApplicationContext()).getTextDate());
20         findViewById(R.id.btnSave).setOnClickListener(new View.OnClickListener() {
21             @Override
22             public void onClick(View view) {
23                 ((App)getApplicationContext()).setTextDate(et.getText().toString());
24                 tv.setText("ShareData: " + et.getText().toString());
25             }
26         });
27     }
28 }
 1 package com.example.metrox.l13;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.EditText;
 7 import android.widget.TextView;
 8 
 9 /**
10  * Created by MetroX on 2016/6/13.
11  */
12 public class Main2 extends Activity {
13     TextView tv;
14     EditText et ;
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.main2);
19         tv = (TextView) findViewById(R.id.textView);
20         et = (EditText)findViewById(R.id.editText);
21         tv.setText("ShareData: " + ((App)getApplicationContext()).getTextDate());
22         findViewById(R.id.btnSave).setOnClickListener(new View.OnClickListener() {
23             @Override
24             public void onClick(View view) {
25                 ((App)getApplicationContext()).setTextDate(et.getText().toString());
26                 tv.setText("ShareData: " + et.getText().toString());
27             }
28         });
29     }
30 }
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.metrox.l13">
 4 
 5     <application
 6         android:name=".App"
 7         android:allowBackup="true"
 8         android:icon="@mipmap/ic_launcher"
 9         android:label="@string/app_name"
10         android:supportsRtl="true"
11         android:theme="@style/AppTheme">
12         <activity android:name=".MainActivity" android:label="Main1">
13             <intent-filter>
14                 <action android:name="android.intent.action.MAIN" />
15 
16                 <category android:name="android.intent.category.LAUNCHER" />
17             </intent-filter>
18         </activity>
19         <activity android:name=".Main2" android:label="Main2">
20             <intent-filter>
21                 <category android:name="android.intent.category.LAUNCHER"></category>
22                 <action android:name="android.intent.action.MAIN"></action>
23             </intent-filter>
24         </activity>
25     </application>
26 
27 </manifest>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <TextView
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:text="New Text"
10         android:id="@+id/textView"
11         android:textSize="20dp" />
12 
13     <EditText
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:id="@+id/editText" />
17 
18     <Button
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:text="保存"
22         android:id="@+id/btnSave" />
23 </LinearLayout>

 

Android Activity全局数据共享

标签:

原文地址:http://www.cnblogs.com/linhongquan/p/5581938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!