java - 使用 MainActivity 中 TextEdit 响应中的整数变量的特定值,以在不同的类中使用?

标签 java android android-studio mobile

我正在尝试编写一个应用程序,该应用程序以 TextEdit 输入的形式从主要 Activity 中获取用户的数字规范,将其转换为整数,然后使用该特定值 的整数并在单独的类文件中使用该值,我将在主 Activity 中使用该类的结果。

这可能吗?这是我在主要 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); 

我不确定如何在 Deadzone 类中使用这些,我正在尝试获取特定值并在那里使用。

编辑:Deadzone.java不是一个 Activity ,而是一个,其中调用的函数MainActivity

最佳答案

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;
}

}

您可以将实例变量的类型更改为 int ... 在目标代码中您可以获得以下数据:

xRef = ClassicSingleton2.getInstance();

这很简单。

2: 以及put(putExtra)的使用

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

在目标代码( 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 
    }

关于java - 使用 MainActivity 中 TextEdit 响应中的整数变量的特定值,以在不同的类中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61769974/

相关文章:

android - :app:compileDebugJava Failed at Android Studio IDE

git - 无法 merge origin/master : error: Your local changes to the following files would be overwritten by merge

repository - 存储库中的 Aar。外部依赖和 NoClassDefFoundError

java - 我们如何为循环创建的多个文本框指定相同的名称

java - 在 Java 绑定(bind)中循环遍历 OpenCV Mat

java run 方法对 arraylist 的每个元素进行操作

android - Firestore 只允许我加载一个文档

java - Hibernate jpa子对象不保存

android - ListView 中的不同行布局

android - phonegap 中的 Gps 精度