简体   繁体   中英

How can I link a string from one to another Class?

I have two Java Classes. "Stickers.java" which is only a Java class. "KeyboardService" which is the activity class.

I have a String inside "Stickers.java" which needs to be overwritten from the activity java class "KeyboardService" by two button.

How can I do it?

I want to change the String "PACK_LIB" from "Stickers.java" by clicking Buttons. The first Button "b1" will overwrite the "PACK_LIB" with "allstickers" and the other button "b2" with "teststickers".

But how can I tell "KeyboardService.java" where the String is. Because it is in a different java class?

// Stickers.java

public void setDefaultStickerPack() {
    checkVersion(true);
    InputStream in = null;
    String packList[]=new String[0];
    String PACK_LIB = "";
    final String PACK_APP="pack_app";
    final String PACK_ICON="pack_on.png";
    String curAssets="";

// KeyboardService.java

    final String[] PACK_LIB = {""};

    final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            PACK_LIB[0] = "allstickers";
        }
    });
    final Button button2 = (Button) mainBoard.findViewById((R.id.b2));
    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            PACK_LIB[0] = "teststickers";
        }
    });

I also tried to import the java class "Stickers.java" into "KeyboardService" . But it doesnt work propably, it will not find the String "Pack_LIB" from "Stickers.java".

PACK_LIB is a local variable to the setDefaultStickerPack() method. Change your code to the following and you should be able to see PACK_LIB from another class.

public String PACK_LIB = "";
public void setDefaultStickerPack() {
    checkVersion(true);
    InputStream in = null;
    String packList[]=new String[0];
    final String PACK_APP="pack_app";
    final String PACK_ICON="pack_on.png";
    String curAssets="";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM