编辑外部类中的变量

时间:2016-02-23 20:51:05

标签: java android

我正在创建一个类似于cookie clicker的应用程序,其中用户可以选择添加升级,这使得您的单位在int dogeNum中的计数增加得更快。升级商店位于与班级不同的布局中,其中存储了dogeNum

主类的代码:

import static java.security.AccessController.getContext;

public class CatMainLayout extends AppCompatActivity {

Random rand = new Random();
SoundPool mySound;
int dogBarking;
boolean aBoolean;
TextView textView;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

@Override


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_layout);
    textView = (TextView) findViewById(R.id.Doge_title);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

    fab.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean dogeSwitch = false;
            if (dogeSwitch = false) {
                dogeSwitch = true;
                Snackbar.make(view, "Many Useless", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            } else if (dogeSwitch = true) {
                dogeSwitch = false;
                Snackbar.make(view, "Many Unimportant", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();


            }
        }
    });
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_cat_main_layout, menu);
    return true;
}

int dogeText;
String dogeDialog;
public void showDogeAlert(View view) {
    aBoolean = rand.nextBoolean();

    if (aBoolean = true) {
        dogeDialog = "Many Android";
    } else if (aBoolean = false) {
        dogeDialog = "Very App";
    } else {
        dogeDialog = "Such Error";
    }
    AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
    myAlert.setMessage(dogeDialog)
            .setPositiveButton("Very Continue", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .setTitle("Much Important Doge Anouncement")
            .setIcon(R.drawable.ic_launcher_doge)
            .create();
    myAlert.show();

}

public void upgradeMenu(View view)
{

}


public void HiddenTreasure(View view) {

    AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
    dogeNum = dogeNum + 100;
    dogeDialog = "Much Special";
    myAlert.setMessage(dogeDialog)
            .setPositiveButton("Much Continue", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .setTitle("Many Important Doge Anouncement")
            .setIcon(R.drawable.gold_treasure_chest)
            .create();
    myAlert.show();

}

int dogeNum = 0; //number of doges
int barkCounter = 0; //not yet added
int multiplierExec = 1; //how many doges a single click will add
int multiplierCost = 100;
String message = "There are ";
public void x2Multiplier(View view)
{
    if (dogeNum >= multiplierCost)
    {

        dogeNum = dogeNum - multiplierCost;
        multiplierExec = multiplierExec * 2;
        textView.setText(message + dogeNum + " Doges!");
        multiplierCost = multiplierCost * 2;
    }else
    {

        dogeDialog = "Not enough doges (you need " + multiplierCost + ")";
        AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
        myAlert.setMessage(dogeDialog)
                .setPositiveButton("Very Continue", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setTitle("Much Important Doge Anouncement")
                .setIcon(R.drawable.ic_launcher_doge)
                .create();
        myAlert.show();
    }
}

public void addDoges(View view) {
    if (dogeNum != 0) {

    }


    dogeNum = dogeNum + multiplierExec;
    barkCounter++;
    textView.setText(message + dogeNum + " Doges!");
    if (barkCounter == 100) {

        barkCounter = 0;
    }
}

public void BarkSound() {

}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onStart() {
    super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "CatMainLayout Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.example.ayden.catmemes/http/host/path")
    );
    AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
    super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "CatMainLayout Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app     activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is     correct.
            Uri.parse("android-          app://com.example.ayden.catmemes/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();
}
}

升级商店类:

public class upgradeMenu extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.upgrade_layout);
    }
}

(我故意不包括进口。)

2 个答案:

答案 0 :(得分:0)

将应用程序中多个Activity应使用的值存储在SharedPreferences

答案 1 :(得分:0)

保存值:

public static final String PREFS_REFERENCE_STRING = "prefs_file";

SharedPreferences.Editor sharedPrefsEditor = getSharedPreferences(PREFS_REFERENCE_STRING, MODE_PRIVATE).edit();
editor.putString("string", "message");
editor.putInt("number", 777);
editor.commit();

检索值:

SharedPreferences prefs = getSharedPreferences(PREFS_REFERENCE_STRING, MODE_PRIVATE); 
String name = prefs.getString("string", "No value");
int number = prefs.getInt("number", 0);

有关详细信息,请参阅http://developer.android.com/reference/android/content/SharedPreferences.html