2012-01-15 71 views
0

這就是我想要對我的應用程序做的事情: 打開總覽屏幕,按計算機按鈕。 比兩個數字相乘。 通過點擊計算,意圖將完成。 乘法結果將顯示在第一個(主要)活動中。將雙數回到之前的活動

我不知道該怎麼做最後一點,有人有什麼想法?

代碼的第一(主)的活性,OverviewpageActivity.java

package com.tip.calc; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 


public class OverviewpageActivity extends Activity { 
private TextView multiplydisplay2; 
private Button btntocalculator; 
private double multiply  = 0; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.overview); 

    multiplydisplay2 = (TextView)findViewById(R.id.multiplydisplay2); 
    btntocalculator = (Button)findViewById(R.id.btntocalculator); 

    btntocalculator.setOnClickListener(new Button.OnClickListener() { 
     public void onClick (View v) { 
      Intent intent = new Intent(OverviewpageActivity.this, 
    TipcalcActivity.class); 
      startActivity(intent); 
      } 
     }); 
    } 
    } 

代碼計算器acticvity,TipcalcActivity.java:

package com.tip.calc; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 


public class TipcalcActivity extends Activity { 
private EditText number1; 
private EditText number2; 
private TextView multiplydisplay; 
private Button btncalculate; 
private Button btnreset; 
private double number1calc = 0; 
private double number2calc = 0; 
private double multiply  = 0; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    initControls(); 
} 

private void initControls() { 
    number1 = (EditText)findViewById(R.id.number1); 
    number2 = (EditText)findViewById(R.id.number2); 
    multiplydisplay = (TextView)findViewById(R.id.multiplydisplay); 
    btncalculate = (Button)findViewById(R.id.btncalculate); 
    btnreset = (Button)findViewById(R.id.btnreset); 
    btncalculate.setOnClickListener(new Button.OnClickListener() { public void 
onClick (View v){ calculate(); }}); 
    btnreset.setOnClickListener(new Button.OnClickListener() { public void 
onClick (View v){ reset(); }}); 
} 

private void calculate() { 
    // check if zero 
    if(number1.getText().toString().trim().length() < 1){number1calc=0;} 
    else{number1calc=Double.parseDouble(number1.getText().toString());} 

    if(number2.getText().toString().trim().length() < 1){number2calc=0;} 
    else{number2calc=Double.parseDouble(number2.getText().toString());} 

//calculate 
    multiply=(number1calc*number2calc); 
    multiplydisplay.setText(Double.toString(multiply)); 
    finish(); 
} 

private void reset() { 
    multiplydisplay.setText(""); 
    number1.setText(""); 
    number2.setText(""); 
    finish(); 
} 
} 

佈局文件,overview.xml:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 



<!-- multiply --> 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:layout_marginTop="20dp" > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="multiply:" /> 
    <TextView 
     android:id="@+id/multiplydisplay2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:layout_marginLeft="20dp" /> 
</LinearLayout> 

<Button 
    android:id="@+id/btntocalculator" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="To the calculator" /> 

</LinearLayout> 

回答

2

在Overviewp中使用startActivityforResult而不是startActivity ageActivity.java並覆蓋OverviewpageActivity.java中的OnactivityResult。

然後在第二個活動中,您可以使用setResult來設置結果。將意圖傳遞給setresult,這將具有雙重價值。 在OnactivityResult你可以得到意圖從中你可以提取雙重

+0

你可以給出示例代碼? @nandeesh – John 2012-01-15 14:32:05

+0

http://remwebdevelopment.com/dev/a33/Passing-Bundles-Around-Activities.html。這裏是示例代碼 – nandeesh 2012-01-15 15:24:15