简体   繁体   English

使用 MainActivity 中 TextEdit 响应中 integer 变量的特定值,以在不同的 class 中使用?

[英]Using specific value of integer variable from TextEdit response in MainActivity, to use in a different class?

I'm attempting to code an app that takes the users numerical specifications from the main activity in the form of a TextEdit input, convert that to an integer and then use that specific value of the integer and use that value in a separate class file which I will use the result of the class in the main activity.我正在尝试编写一个应用程序,该应用程序以TextEdit输入的形式从主要活动中获取用户的数字规范,将其转换为 integer,然后使用 integer 的特定值并在单独的 ZA2F2ED4F8EBC2B61DDC2 文件中使用该值我将在主要活动中使用 class 的结果。

Is this possible?这可能吗? Here's what I've attempted in the global variables of the main activity:这是我在主要活动的全局变量中尝试的内容:

deadzoneValue = findViewById(R.id.TextView_deadzoneInfo);
public EditText threshold, deadzone;

public String deadzoneString = deadzone.getText().toString(); //deadzone being the name of the 
public int timeLimit = Integer.parseInt(deadzoneString);

public String thresholdString = threshold.getText().toString();
public static int thresholdLimit = Integer.parseInt(thresholdString); 

I'm not sure how to use these in the Deadzone class, which I'm trying to take the specific value and use there.我不知道如何在Deadzone class 中使用这些,我正在尝试获取特定值并在那里使用。

EDIT : Deadzone.java isn't an activity but a class with functions that are called in the MainActivity .编辑Deadzone.java不是一个activity ,而是一个class具有在MainActivity中调用的函数。

Using of ClassicSingleton:使用 ClassicSingleton:

public class ClassicSingleton2 {
private static String instance = null;

protected ClassicSingleton2() {

}

public static String getInstance() {
    return instance;
}

public static void setInstance(String instance) {
    ClassicSingleton2.instance = instance;
}

} }

You could change type of instance variable to int... And in target code you could get this data:您可以将实例变量的类型更改为 int... 在目标代码中您可以获得以下数据:

xRef = ClassicSingleton2.getInstance();

This is very simple.这很简单。

2: And using of put (putExtra) 2: 并使用 put (putExtra)

Intent oI = new Intent((FirstActivity)this,SecondActivity.class);
            oI.putExtra("XRefCaller",123);

And in target code(activity):在目标代码(活动)中:

Bundle oBundle = getIntent().getExtras();
    if(oBundle != null){
        oXRefCaller = oBundle.getString("XRefCaller",-1);
        //checking with -1 if the parameter does not exist or is null 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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