2

我在共享需要跨多个活动保留的全局变量时遇到问题。此变量在 TextPlay 类中创建并复制到 DailyDataEntry 类中。我已经使用 Intent 在两个活动之间共享变量进行了一些研究,但我希望将该变量共享给多个类。我想我将不得不扩展 Application 但我所有的其他类已经在扩展 Activity 了。我应该如何在扩展 Activity 的同时扩展 Application?太感谢了

///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++;
        }
    });

}
}
4

1 回答 1

3

创建将Application在需要时以下列方式扩展并使用它的类:

MyApplication myAppHelper = (MyApplication) context.getApplication();
myAppHelper.setWhatever(whatever);
Whatever whatever = myAppHelper.getWhatever();
于 2013-01-22T02:10:30.613 回答