代码之家  ›  专栏  ›  技术社区  ›  canyon289

非常简单的Android菜单问题

  •  1
  • canyon289  · 技术社区  · 15 年前

    我有一个关于Android中菜单控制的非常简单的问题。我正在编写一个程序,它执行一些简单的数值计算并输出一个答案。我的问题是,当按下一个按钮时,如何使程序移到第二个屏幕,以及如何让用户移回原始屏幕。 下面是一个简单的例子

    屏幕1

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

    屏幕2

    The answer is "____"
    

    用户输入两个整数。按添加,然后程序移动到显示答案的第二个屏幕。如果用户愿意,他们可以返回第一个屏幕重新开始。

    我知道这已经被覆盖了,但是有这么多资源我不知道该寻找什么。答案或资源链接将是最有用的。谢谢您!

    4 回复  |  直到 15 年前
        1
  •  3
  •   Primal Pappachan    15 年前

    您可以使用以下屏幕布局输入数字。将文件命名为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);
        }
    }
    

    希望这有帮助。

        2
  •  1
  •   strager    15 年前

    我能知道你用哪种语言吗? 我最近刚用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的IntelliSense,应该能够获得宽度、高度等。 无法准确记住,但类似于src.width或src.bounds.width的内容。

    移动程序的最简单方法是将程序X轴移动到相应的屏幕。

        3
  •  1
  •   NetApex    15 年前

    视图翻转器工作得很好。您还可以尝试“StartActivityForResult”将答案传递到下一个屏幕。

        4
  •  0
  •   jacknad    15 年前

    看看 ViewFlipper 班级。它的工作方式与标签类似,但没有标签用户界面。可以使用XML布局显示两个(或更多)屏幕,以及 showNext () showPrevious ()在屏幕之间切换。