2010-08-08 42 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”将答案传递到下一个屏幕。