2

複数のアクティビティで保持する必要があるグローバル変数の共有に問題があります。この変数は TextPlay クラスで作成され、DailyDataEntry クラスにコピーされます。Intent を使用して 2 つのアクティビティ間で変数を共有するいくつかの調査を行いましたが、その変数を複数のクラスで共有しようとしています。Application を拡張する必要があると思いますが、他のすべてのクラスは既に 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++;
        }
    });

}
}
4

1 に答える 1

3

に拡張するクラスを作成Applicationし、必要なときに次の方法で使用します。

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