2016-10-01 62 views
-1

主要活動如下:在Android Studio中,如何在下一個屏幕上獲得計算結果?

package net.androidbootcamp.twitterstockcalculator; 

import android.content.Intent; 
import android.icu.text.DecimalFormat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Button; 
import android.widget.TextView; 

import static net.androidbootcamp.twitterstockcalculator.R.id.btnCost; 
import static net.androidbootcamp.twitterstockcalculator.R.id.txtResult; 


public class MainActivity extends AppCompatActivity { 

double costPerShare = 23.01; 
int numberOfShares; 
double totalCost; 
//String one = "Cost for the Twitter Stock:"; 
//String two = "."; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final EditText number = (EditText)findViewById(R.id.txtNumber); 
    Button button = (Button) findViewById(btnCost); 

    button.setOnClickListener(new View.OnClickListener() { 
     // final TextView result = ((TextView)findViewById(txtResult)); 
     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(MainActivity.this,                TotalSharePrice.class)); 
      numberOfShares = Integer.parseInt(number.getText().toString()); 
      totalCost = costPerShare * numberOfShares; 
      DecimalFormat currency = new DecimalFormat("$###.##"); 
      // final TextView result = ((TextView)findViewById(txtResult)); 
      // result.setText("Cost to buy Twitter Stock: $" + currency.format(totalCost)); 




    *the second activity looks like this: 


    package net.androidbootcamp.twitterstockcalculator; 

    import android.content.Intent; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.TextView; 

    import static net.androidbootcamp.twitterstockcalculator.R.id.btnCost; 
    import static net.androidbootcamp.twitterstockcalculator.R.id.txtResult; 


    public class TotalSharePrice extends AppCompatActivity { 
    //String groupChoice; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_total_share_price); 

    final TextView result = ((TextView)findViewById(R.id.txtResult)); 
    result.setText("Cost to buy Twitter Stock: $" + result); 

我怎樣才能查看下一個屏幕上的計算結果?

我正在做股票計算器,它只是不會顯示計算結果。

+0

注意[我們不鼓勵緊急請求](http://meta.stackoverflow.com/q/326569),在標題中更是如此,它們是問題破壞的一種形式。請不要將此添加到您未來的帖子中,謝謝。 – halfer

回答

1

您需要將這些數據傳遞給您的意圖的第二個活動。請參閱this回答。

相關問題