gpt4 book ai didi

java - 在运行时更新默认值并在 android studio 中使用新值进行计算

转载 作者:太空宇宙 更新时间:2023-11-04 09:47:58 24 4
gpt4 key购买 nike

所以我有三项 Activity 。其中一个用于接受用户的输入,如果所有输入均已填写,则使用下一步按钮移动到第二个 Activity 。

第二个 Activity 可用于接受和更新第二个类中用于执行计算的一些已定义的值,保存按钮用于保存新值,完成按钮用于执行计算并在上一个 Activity 中显示结果。

每次我在输入后单击保存按钮决定使用一组新值时,如何更新第二个 Activity/类?

第一个 Activity

<EditText
android:id="@+id/first_number"
android:layout_width="304dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/edittext_bg"
android:ems="10"
android:hint="Enter first Number"
android:inputType="number"
android:paddingStart="20dp"
android:paddingEnd="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textSize="14sp" />

<EditText
android:id="@+id/second_number"
android:layout_width="304dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/edittext_bg"
android:ems="10"
android:hint="Enter second Number"
android:inputType="number"
android:paddingStart="20dp"
android:paddingEnd="10dp"/>

<EditText
android:id="@+id/third_number"
android:layout_width="304dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/edittext_bg"
android:ems="10"
android:hint="Enter second Number"
android:inputType="number"
android:paddingStart="20dp"
android:paddingEnd="10dp"/>

<Button
android:id="@+id/btn_next"
android:layout_width="145dp"
android:layout_height="49dp"
android:layout_marginTop="8dp"
android:background="@drawable/curved_button"
android:paddingStart="5dp"
android:paddingEnd="19dp"
android:paddingRight="10dp"
android:paddingLeft="2dp"
android:drawableLeft="@drawable/add_button_image"
android:text="@string/Next"
android:textAllCaps="false"
android:textColor="@android:color/background_light"
android:typeface="serif"
app:layout_constraintEnd_toEndOf="parent" />


SECOND ACTIVITY
<EditText
android:id="@+id/contingencyFee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:background="@color/pageColor"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="@string/contingency"
android:inputType="none"
android:textSize="16sp" />

<EditText
android:id="@+id/projectManagement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:background="@color/pageColor"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="@string/projectFee"
android:inputType="none"
android:textSize="16sp" />

<EditText
android:id="@+id/externalWorks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:background="@color/pageColor"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="@string/externalWorks"
android:inputType="none"
android:textSize="16sp" />

<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="38dp"
android:layout_marginStart="120dp"
android:layout_marginLeft="120dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="52dp"
android:onClick="saveButton"
android:background="@drawable/save_button"
android:text="@string/inputVariableSaveButton"
android:textColor="@android:color/background_light"
app:layout_constraintBottom_toTopOf="@+id/textView16"
app:layout_constraintEnd_toStartOf="@+id/proceedBtn"
app:layout_constraintHorizontal_bias="0.464"
app:layout_constraintTop_toBottomOf="@+id/scrollView3"
app:layout_constraintVertical_bias="0.483" />

<Button
android:id="@+id/proceed_button"
android:layout_width="116dp"
android:layout_height="32dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="52dp"
android:onClick="proceed"
android:text="@string/defaultVariable"
android:textAllCaps="false"
android:textColor="@android:color/background_light"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/textView16"
app:layout_constraintEnd_toEndOf="parent"

FIRST CLASS

btn_next.setOnClickListener(new View.OnClickListener(){
@RequiresApi(api = Build.VERSION_CODES.N)
public void onClick(View v){
try{

// **** VALUES FROM THE FIRST ACTIVITY

double first_number =
Double.valueOf(first_number.getText().toString());
double firstNumberTotal = first_number* /*
(250*70000)*/ 17500000;

double second_number =
Double.valueOf(second_number.getText().toString());
double secondNumberTotal = second_number * /*
(214*70000)*/ 14980000;

double third_number =
Double.valueOf(third_number.getText().toString());
double thirdNumberTotal = third_number * /*
(140*70000)*/ 9800000;

}catch (Exception ex){
return;
}
}
});


SECOND CLASS
save_button.setOnClickListener(new View.OnClickListener(){
@RequiresApi(api = Build.VERSION_CODES.N)
public void onClick(View v){
try{

// VALUES FROM SECOND ACTIVITY
double contingencyFee =
Double.valueOf(contingencyFeet.getText().toString());
double contingencyTotal = contingencyFee *
1.015; /*UPDATE THIS WITH NEW VALUE*/


double projectManagement =
Double.valueOf(projectManagement.getText().toString());
double totalProjectCost = projectManagemen *
1.2; /*UPDATE THIS WITH NEW VALUE*/

double externalWorks =
Double.valueOf(externalWorks.getText().toString());
double externalWorksCost = externalWorks *
1.15; /*UPDATE THIS WITH NEW VALUE*/

}catch (Exception ex){
return;
}
}
});

最佳答案

按钮点击中的第一个 Activity

intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("firstno", String.valueOf(firstnumber));
intent.putExtra("secondno", String.valueOf(secondnumber));
intent.putExtra("thirdno", String.valueOf(thirdnumber));
startActivity(intent);

加载第二个 Activity

String firstNo, SecondNo, ThirdNo;
firstNo = getIntent().getExtras().getString("firstno");
SecondNo = getIntent().getExtras().getString("secondno");
ThirdNo = getIntent().getExtras().getString("thirdno");

编辑内容

Double firstNo, SecondNo, ThirdNo;
firstNo = Double.parseDouble(getIntent().getExtras().getString("firstno"));
SecondNo = Double.parseDouble(getIntent().getExtras().getString("secondno"));
ThirdNo = Double.parseDouble(getIntent().getExtras().getString("thirdno"));

textView.setText(String.valueOf(firstNo + SecondNo + ThirdNo));

关于java - 在运行时更新默认值并在 android studio 中使用新值进行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55185447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com