2010-08-08 152 views
1

我有一個關於Android中的菜單控制一個非常簡單的問題。我正在編寫一個程序,執行一些簡單的數字計算並輸出答案。我的問題是如何讓程序在按下按鈕時移動到第二個屏幕,以及如何讓用戶移回到原始屏幕。 下面是一個簡單的例子非常簡單的菜單問題的Android

屏幕1

"Add Numbers" 

Input 1st # ____  
Input 2nd # ____  
(Add) 

屏幕2所

The answer is "____" 

用戶輸入的兩個整數。按下添加,然後程序移動到顯示答案的第二個屏幕。如果用戶想要他們可以返回到第一個屏幕並重新開始。

我知道這已被覆蓋,但有這麼多的資源,我不知道該找什麼。答案或資源鏈接將是最有幫助的。謝謝!

+1

什麼語言/平臺? – jwsample 2010-08-08 00:51:21

+1

您使用的是什麼平臺,語言和技術?控制檯輸入/輸出和C? Windows上的.NET WPF? – 2010-08-08 00:52:23

回答

3

您可以在屏幕上使用以下佈局來輸入數字。將文件命名爲first.xml。

<TextView 
     android:id="@+id/firstnumber" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Input Ist:" 
     android:textSize="20px" 
     android:textStyle="bold" 
     /> 

<EditText 
     android:id="@+id/first" 
     android:layout_height="wrap_content" 
     android:layout_width="250px" 

    /> 

    <TextView 
     android:id="@+id/secondnumber" 
     android:text = "Input 2nd" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="20px" 
     android:textStyle="bold" 
     />  

    <EditText 
       android:id="@+id/second" 
        android:layout_height="wrap_content" 
        android:layout_width="250px" 

     /> 

    <Button 
      android:id="@+id/add_button" 
       android:text="Add" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="15px" 
       android:layout_centerHorizontal="true" 

       /> 

</LinearLayout> 

要添加這些數字

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.first); 
     Button add = (Button) findViewById(R.id.add); 
     add.setOnClickListener(this); 
     EditText num1 = (EditText)findViewById(R.id.first); 
     EditText num2 = (EditText)findViewById(R.id.second); 
} 

的點擊監聽器的代碼

public void onClick(View v) { 

       int n1 = Integer.parseInt(num1.getText().toString()); 
       int n2 = Integer.parseInt(num2.getText().toString()); 
       int result = n1 + n2; 
       String res = Integer.toString(result); 
           Intent intent = new Intent(getApplicationContext(), Result.class); 
           intent.putExtra("result", result); //Passing the result to the second activity using intent 
       startActivity(intent); 
       } 

在Result.java類。

public class Result extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.result); 
      Intent intent = getIntent(); 
       String result = intent.getStringExtra("result"); 
       TextView t1 = (TextView)findViewById(R.id.result); 
       t1.setText(result); 
    } 
} 

希望這有助於..

+0

絕對是。你給了我一個我需要的直接例子。這個起點非常有幫助。非常感謝你! – canyon289 2010-08-08 04:24:48

1

我可以知道你使用哪種語言?我最近剛剛用C#做了這個。 我的流程是,當我啓動一個程序時,它會自動被強制轉到第二個屏幕,並帶有一些電源選項。

但是,如果你的程序只是將程序移動到diff屏幕上,那麼在C#中很容易。

Screen Srn = Screen.PrimaryScreen; // gives your information of Primary Screen. 

Screen[] Srn = Screen.AllScreens; //gives u an array of all your available screen(srn[0] is primary screen, etc) 

因此,使用智能感知從IDE,應該能夠得到寬度,高度等 廣東話準確記得,但類似src.width或src.bounds.width。

最簡單的移動程序的方法是將程序x軸移動到相應的屏幕。

0

看看ViewFlipper課程。它像標籤一樣工作,但沒有標籤UI。您可以使用xml佈局顯示您的2個(或更多)屏幕,並使用showNext()或showPrevious()在屏幕之間翻轉。

+0

非常感謝您的鏈接!我現在已經閱讀了文檔,我將重新閱讀它以獲得充分的理解。 – canyon289 2010-08-08 04:25:18

1

ViewFlipper將運行良好。您也可以嘗試「startActivityForResult」將答案傳遞到下一個屏幕。