我是Android新手并尝试开发一个简单的Android应用程序。我试图将来自不同活动的两个值作为参数发送给第三个。问题是我使用这两个值的方法没有看到用户输入的值。提前致谢。 该活动中的代码是:
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);
}
YourExpenses.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);
}
}
答案 0 :(得分:2)
您是否只是想调用公共功能?这不起作用,因为“通常”一次只有一个Activity具有前景。您可以使用Intent作为额外值传递值,使用共享首选项,最极端扩展应用程序并使用它来存储所有数据。