如何在按钮单击事件中返回特定方法?

时间:2019-01-03 10:18:43

标签: java android android-studio button methods

两个按钮应该更新“ Stickers.java”中的值“ PACK_LIB”。

当他们覆盖String时,应该重新启动setDefaultStickerPack()方法。

单击按钮b1或b2时,值“ PACK_LIB”将被值“ allstickers”或“ teststickers”覆盖。

按钮b1或b2如何在setDefaultStickerPack()内部重新启动方法“ if(in == null)”?

------- KeyboardService.java

 final Button button2 = (Button) mainBoard.findViewById(R.id.b2);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Stickers.PACK_LIB = "allstickers";
                stickers.setDefaultStickerPack();
                showStickers();
            }
        });

        final Button button3 = (Button) mainBoard.findViewById(R.id.b3);
        button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Stickers.PACK_LIB = "teststickers";
                stickers.setDefaultStickerPack();
                showStickers();
            }
        });

------- Stickers.java

public static String PACK_LIB ="";


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


    try {
        in = lContext.getAssets().open(PACK_APP+"/"+PACK_ICON);
        curAssets=PACK_APP;
        packList = lContext.getAssets().list(curAssets);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if(in==null) {
        try {
            in = lContext.getAssets().open(PACK_LIB+"/"+PACK_ICON);
            curAssets=PACK_LIB;
            packList = lContext.getAssets().list(curAssets);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (in != null) {
        long packId = 1;
        PackData packData = new PackData();
        packData.objectId = packId;
        packData.name = "ROKOmoji";
        packData.iconOn = copyImgFile(in, "i" + packId + "_on");
        //packData.iconOff = copyImgFile(inOff, "i" + packId + "_off");
        List<StickerData> stickerData = new ArrayList<StickerData>();
        long i = 0;
        for (String img: packList) {
            if(PACK_ICON.equals(img)){
                continue;
            }
            InputStream sIs = null;
            try {
                sIs = lContext.getAssets().open(curAssets+"/"+img);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (sIs != null) {
                StickerData sd = new StickerData();
                i=i+1;
                File file = copyImgFile(sIs, "s" + img);
                sd.objectId = i;
                sd.imageId = i;
                sd.packId = packId;
                sd.packName = packData.name;
                sd.file = file;
                sd.iconKey = createIconKey(file, "si" + img);
                sd.mime = getMimeTypeOfFile(file.getPath());//"image/gif"
                sd.url = null;
                stickerData.add(sd);
            }
        }
        packData.stickers = stickerData;
        packDataListDefault.add(packData);
    }


}

1 个答案:

答案 0 :(得分:0)

U可以使用参数调用该方法。尝试这个。

------- KeyboardService.java

final Button button2 = (Button) mainBoard.findViewById(R.id.b2);
    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            stickers.setDefaultStickerPack("allstickers");
            showStickers();
        }
    });

    final Button button3 = (Button) mainBoard.findViewById(R.id.b3);
    button3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            stickers.setDefaultStickerPack("teststickers");
            showStickers();
        }
    });

------- Stickers.java

private static String PACK_LIB ="";

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

PACK_LIB = packLibValue

try {
    in = lContext.getAssets().open(PACK_APP+"/"+PACK_ICON);
    curAssets=PACK_APP;
    packList = lContext.getAssets().list(curAssets);
} catch (IOException e) {
    e.printStackTrace();
}
if(in==null) {
    try {
        in = lContext.getAssets().open(PACK_LIB+"/"+PACK_ICON);
        curAssets=PACK_LIB;
        packList = lContext.getAssets().list(curAssets);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

..........
..........

}