Advertisement
cisekluk

Untitled

Jul 6th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. /**
  2. *   Klasa posiadajaca zmienne globalne
  3. */
  4. package com.example.apps;
  5.  
  6. import android.app.Application;
  7.  
  8. public class Globals extends Application {
  9.    
  10.     private String str;
  11.  
  12.     public String getStr() {
  13.         return str;
  14.     }
  15.  
  16.     public void setStr(String str) {
  17.         this.str = str;
  18.     }  
  19. }
  20.  
  21. /**
  22. *   Klasa ustawiajaca wartosc dla zmiennej globalnej
  23. */
  24. public class CustomVar extends Activity {
  25.    
  26.     private Button btn;
  27.     Globals g;
  28.    
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_setters);
  33.        
  34.         g = ((Globals)getApplicationContext());
  35.        
  36.        
  37.         btn = (Button)findViewById(R.id.btnSets);
  38.         btn.setOnClickListener(new View.OnClickListener() {
  39.            
  40.             @Override
  41.             public void onClick(View v) {
  42.                 g.setStr("test");
  43.                
  44.             }
  45.         });    
  46.     }
  47. }
  48.  
  49. /**
  50. *   Główna klasa
  51. **/
  52.  
  53. public class MainActivity extends Activity {
  54.  
  55.     private Button btn2;
  56.     private TextView tv;
  57.     private Button btn3;   
  58.     private Globals g;
  59.    
  60.     @Override
  61.     protected void onCreate(Bundle savedInstanceState) {
  62.         super.onCreate(savedInstanceState);
  63.         setContentView(R.layout.activity_main);
  64.        
  65.         g = ((Globals)getApplicationContext());
  66.        
  67.         tv = (TextView)findViewById(R.id.textView1);
  68.         btn2 = (Button)findViewById(R.id.btnGet);
  69.         btn2.setOnClickListener(new View.OnClickListener() {
  70.            
  71.             @Override
  72.             public void onClick(View v) {
  73.                 tv.setText(g.getStr());
  74.                
  75.             }
  76.         });
  77.        
  78.        
  79.         btn3 = (Button)findViewById(R.id.btn3);
  80.         btn3.setOnClickListener(new View.OnClickListener() {
  81.            
  82.             @Override
  83.             public void onClick(View v) {
  84.                 Intent i = new Intent(getApplicationContext(), Setters.class);
  85.                 startActivity(i);
  86.                
  87.             }
  88.         });    
  89.     }  
  90. }
  91.  
  92. /**
  93. *   manifest
  94. */
  95. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  96.     package="com.example.apps"
  97.     android:versionCode="1"
  98.     android:versionName="1.0" >
  99.  
  100.     <uses-sdk
  101.         android:minSdkVersion="8"
  102.         android:targetSdkVersion="17" />
  103.  
  104.     <application
  105.         android:name="com.example.apps.Globals"
  106.         android:allowBackup="true"
  107.         android:icon="@drawable/ic_launcher"
  108.         android:label="@string/app_name"
  109.         android:theme="@style/AppTheme" >
  110.         <activity
  111.             android:name="com.example.apps.MainActivity"
  112.             android:label="@string/app_name" >
  113.             <intent-filter>
  114.                 <action android:name="android.intent.action.MAIN" />
  115.  
  116.                 <category android:name="android.intent.category.LAUNCHER" />
  117.             </intent-filter>
  118.         </activity>
  119.         <activity
  120.             android:name="com.example.apps.CustomVar"
  121.             android:label="@string/title_activity_setters" >
  122.         </activity>
  123.         <activity
  124.             android:name="com.example.apps.Second"
  125.             android:label="@string/title_activity_second" >
  126.         </activity>
  127.     </application>
  128.  
  129. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement