我正在尝试编写一个应用程序,该应用程序以 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/