2013-01-22 44 views
2

我有共享問題需要跨多個活動保留的全局變量。此變量在TextPlay類中創建並複製到DailyDataEntry類中。我使用Intent在兩個活動之間做了一些研究共享變量,但我希望將該變量分享給多個類。我想我將不得不擴展應用程序,但所有其他類都已經擴展了Activity。我如何在擴展活動的同時擴展應用程序?萬分感謝如何在擴展的同時使用應用程序類來共享變量活動類

/// TextPlay類///

package com.armstrong.y.android.app; 

import java.util.Random; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.text.InputType; 
import android.view.Gravity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.ToggleButton; 

public class TextPlay extends Activity implements View.OnClickListener { 

Button chkCmd; 
ToggleButton passTog; 
EditText input; 
TextView display; 

EditText etUserId; 
Button btnUserId; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.text); 

    baconAndEggs(); 
    passTog.setOnClickListener(this); 
    chkCmd.setOnClickListener(this); 

    // Share variables between classes controls 
    etUserId = (EditText) findViewById(R.id.etUserId); 
    btnUserId = (Button) findViewById(R.id.btnUserId); 

} 

private void baconAndEggs() { 
    // TODO Auto-generated method stub 
    chkCmd = (Button) findViewById(R.id.bResults); 
    passTog = (ToggleButton) findViewById(R.id.tbPassword); 
    input = (EditText) findViewById(R.id.etCommands); 
    display = (TextView) findViewById(R.id.tvResults); 
} 

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.bResults: 
     // get the text from the command box then convert it to string 
     String check = input.getText().toString(); 
     display.setText(check); 
     if (check.contentEquals("left")) { 
      display.setGravity(Gravity.LEFT); 
     } else if (check.contentEquals("center")) { 
      display.setGravity(Gravity.CENTER); 
     } else if (check.contentEquals("right")) { 
      display.setGravity(Gravity.RIGHT); 
     } else if (check.contentEquals("blue")) { 
      display.setTextColor(Color.BLUE); 
      display.setText("blue"); 
     } else if (check.contains("hellow")) { 
      Random crazy = new Random(); 
      display.setText("hello!!!!"); 
      display.setTextSize(crazy.nextInt(75)); 
      display.setTextColor(Color.rgb(crazy.nextInt(255), 
        crazy.nextInt(255), crazy.nextInt(255))); 
      switch (crazy.nextInt(3)) { 
      case 0: 
       display.setGravity(Gravity.LEFT); 
       break; 
      case 1: 
       display.setGravity(Gravity.CENTER); 
       break; 
      case 2: 
       display.setGravity(Gravity.RIGHT); 
       break; 
      } 
     } else { 
      display.setText("invalid"); 
      display.setGravity(Gravity.CENTER); 
      display.setTextColor(Color.BLACK); 
     } 
     break; 
    case R.id.tbPassword: 
     // If toggle button is set to ON, password will be *** 
     if (passTog.isChecked()) { 
      input.setInputType(InputType.TYPE_CLASS_TEXT 
        | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
     } else { 
      // if toggle is off, its going be plain text 
      input.setInputType(InputType.TYPE_CLASS_TEXT); 
     } 
     break; 
    } 
} 
} 

/// DailyDataEntry類///

package com.armstrong.y.android.app; 

import java.text.SimpleDateFormat; 
import java.util.Calendar; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class DailyDataEntry extends Activity { 
String strdate; 
EditText etGetDate; 
Calendar calendarDateToday = Calendar.getInstance(); 
Calendar calendarDateNext = Calendar.getInstance(); 
Calendar calendarDatePrevious = Calendar.getInstance(); 
int counterNext = 0; 
int counterPrevious = 0; 
Button btnTodayDate; 
Button btnPreviousDate; 
Button btnGetNextDate; 

EditText etDailyCaloriesIn; 
EditText etDailyCaloriesOut; 

EditText etDailyAnxietyLevel; 
EditText etDailySleepQuality; 
TextView tvUserId; 
Button btnSubmit; 
Button btnReset; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.daily_data_entry); 

    // Toast Message Warning 
    Toast.makeText(getApplicationContext(), 
      "DO NOT LEAVE FIELD BLANK OR CRASH IS ON YOU!", 
      Toast.LENGTH_LONG).show(); 

    btnTodayDate = (Button) findViewById(R.id.btnGetTodayDate); 
    btnPreviousDate = (Button) findViewById(R.id.btnGetPreviousDate); 
    btnGetNextDate = (Button) findViewById(R.id.btnGetNextDate); 
    etGetDate = (EditText) findViewById(R.id.etGetDate); 
    btnSubmit = (Button) findViewById(R.id.btnSubmit); 
    btnReset = (Button) findViewById(R.id.btnReset); 

    etDailyCaloriesIn = (EditText) findViewById(R.id.etDailyCaloriesIn); 
    etDailyCaloriesOut = (EditText) findViewById(R.id.etDailyCaloriesOut); 

    etDailyAnxietyLevel = (EditText) findViewById(R.id.etDailyAnxietyLevel); 
    etDailySleepQuality = (EditText) findViewById(R.id.etDailySleepQuality); 

    tvUserId = (TextView) findViewById(R.id.tvUserId); 


    // When Submit Button is clicked 
    btnSubmit.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Grab User Input from EditText and Convert input String to 
      // Integer 
      int check1 = Integer.parseInt(etDailyCaloriesIn.getText() 
        .toString()); 
      int check2 = Integer.parseInt(etDailyCaloriesOut.getText() 
        .toString()); 

      int check4 = Integer.parseInt(etDailyAnxietyLevel.getText() 
        .toString()); 
      int check5 = Integer.parseInt(etDailySleepQuality.getText() 
        .toString()); 

      strdate = etGetDate.getText().toString(); 

      // Run Legal Value Integrity Check 
      if (strdate.length() != 8) { 
       etGetDate.setText("Please enter in the correct format."); 
      } else if (strdate.equals("")) { 
       etGetDate.setText("Please enter a date."); 
      } else if (check1 > 10 || check1 < 1) { 
       etDailyCaloriesIn.setText("Incorrect Value!"); 
      } else if (check2 > 10 || check2 < 1) { 
       etDailyCaloriesOut.setText("Incorrect Value!"); 
      } else if (check4 > 10 || check4 < 1) { 
       etDailyAnxietyLevel.setText("Incorrect Value!"); 
      } else if (check5 > 10 || check5 < 1) { 
       etDailySleepQuality.setText("Incorrect Value!"); 
      } else { 
       etGetDate.setText("Submited!"); 
       etDailyCaloriesIn.setText("Submited!"); 
       etDailyCaloriesOut.setText("Submited!"); 

       etDailyAnxietyLevel.setText("Submited!"); 
       etDailySleepQuality.setText("Submited!"); 
      } 
     } 
    }); 

    // When Reset Button is Clicked 
    btnReset.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      String empty = ""; 
      etDailyCaloriesIn.setText(empty); 
      etDailyCaloriesOut.setText(empty); 

      etDailyAnxietyLevel.setText(empty); 
      etDailySleepQuality.setText(empty); 
      etGetDate.setText(empty); 
     } 
    }); 

    // Capture Today's Date 
    btnTodayDate.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy"); 
      strdate = sdf.format(calendarDateToday.getTime()); 
      etGetDate.setText(strdate); 
     } 
    }); 

    // Capture Previous's Date 
    btnPreviousDate.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy"); 

      if (counterPrevious == 0) { 
       calendarDatePrevious.add(Calendar.DATE, -1); 
      } 
      strdate = sdf.format(calendarDatePrevious.getTime()); 

      etGetDate.setText(strdate); 

      counterPrevious++; 
     } 
    }); 

    // Capture Next's Date 
    btnGetNextDate.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy"); 

      if (counterNext == 0) { 
       calendarDateNext.add(Calendar.DATE, +1); 
      } 

      strdate = sdf.format(calendarDateNext.getTime()); 
      etGetDate.setText(strdate); 
      counterNext++; 
     } 
    }); 

} 
} 

回答

3

創建類,將延伸到Application,並在下面使用方式,只要你需要它:

MyApplication myAppHelper = (MyApplication) context.getApplication(); 
myAppHelper.setWhatever(whatever); 
Whatever whatever = myAppHelper.getWhatever(); 
相關問題