1
我是新来的Android,并试图开发一个简单的Android应用程序。我试图将来自不同活动的两个值作为参数发送给第三个参数。问题是我使用这两个值的方法没有看到用户输入的值。提前致谢。 该活动的代码是:在java中接收参数问题
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);
}
}
你期望填充什么值?请更具体一些。 – twaddington
感谢您的回答。这些值应该由用户输入。当用户输入他/她想要花费的金额和物品价格时,我期望结果是(金额 - 物品价格)。但即使getItemPrice()和getUserAmountEntered()方法打印用户输入的实际金额,它仍会将结果返回为零。 – user1172575
我没有看到你在这个Activity中调用这些方法的地方。你需要做的是在你的布局中创建一个提交'Button'并将一个'OnClickHandler'绑定到它。当你的'OnClick'方法被调用时,你可以从你的文本字段中读取值。您不能在另一个Activity上调用方法。另外,请使用'Log'类而不是'System.out.println':http://developer.android.com/reference/android/util/Log.html – twaddington