1

我是 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);

            }
        }
4

1 回答 1

2

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

于 2012-06-07T20:30:51.897 回答