gpt4 book ai didi

java - 在java中接收参数问题

转载 作者:行者123 更新时间:2023-11-29 22:05:00 25 4
gpt4 key购买 nike

我是 Android 的新手,正在尝试开发一个简单的 Android 应用程序。我正在尝试将来自不同 Activity 的两个值作为参数发送给第三个值。问题是我使用这两个值的方法没有看到用户输入的值。提前致谢。该 Activity 中的代码是:

public class NewAcount extends Activity {

private DecimalFormat twoDigits = new DecimalFormat("0.00");
TextView final_Amount;
EditText price, amount_Entered;
double userr_Entered_Amount, itemPricee, result;

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.transparent);
// amount of money the user wants to spend daily
amount_Entered = (EditText) findViewById(R.id.et_Amount);
// item price
price = (EditText) findViewById(R.id.et_Price);

final_Amount = (TextView) findViewById(R.id.tv_Account_Result);
// set the text view value
final_Amount.setText(twoDigits.format(getTotal()));

}

public double getItemPrice(double itemPrice) {
this.itemPricee = itemPrice;
System.out.println("the item price in the new account activity is "
+ itemPricee);

return itemPricee;
}

public double getUserAmountEntered(double user_Entered_Amount) {

this.userr_Entered_Amount = user_Entered_Amount;
System.out.println("the user amount in the new account activity is "
+ userr_Entered_Amount);

return userr_Entered_Amount;

}

public double getTotal() {
// Here where the problem is. it's not seeing
// userr_Entered_Amount and itemPricee
// and returns result as zero

result=getUserAmountEntered(userr_Entered_Amount)-getItemPrice(itemPricee);

System.out.println("the result is " + result);
return result;
}

}

FinanceAppActivity.java我正在发送输入的金额:

    if (amount_Entered.getText().toString().length() > 0) {
// start your expenses app activity
Intent openYourExpensesActivity = new Intent(
"android.intent.action.FINANCESQLLITE");
startActivity(openYourExpensesActivity);
double user_Entered_Amount=Double.parseDouble(amount_Entered.getText().toString());
//send amount entered to new account activity
NewAcount na=new NewAcount();
na.getUserAmountEntered(user_Entered_Amount);
}

你的费用.java发送商品价格

    // to determine which button was clicked
switch (v.getId()) {
case R.id.btn_Add:
boolean didItWork = true;
try {
String itemName = item.getText().toString();
itemPrice = Double.parseDouble(price.getText().toString());
Price entry = new Price(YourExpenses.this);
entry.open();
// passing both strings to create entry method
entry.creatEntry(itemName, itemPrice);
entry.close();
NewAcount na=new NewAcount();
//send item price to new account activity
na.getItemPrice(itemPrice);
} catch (Exception e) {
//code here
} finally {

if (didItWork) {
Dialog d = new Dialog(this);
d.setTitle("Inserted successfully!");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Intent openNewAccount = new Intent("android.intent.action.TRANSPARENT");
startActivity(openNewAccount);

}
}

最佳答案

您只是想调用公共(public)函数吗?这是行不通的,因为“通常”一次只有一个 Activity 有前景。您可以将带有 Intent 的值作为附加值传递,使用最极端扩展应用程序的共享首选项,并使用它来存放您的所有数据。

关于java - 在java中接收参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10939345/

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