你好,游客 登录 注册 搜索
背景:
阅读新闻

Android应用程序的开发目录——简单的加法计算示例

[日期:2011-10-12] 来源:csdn  作者:jiyuhang [字体:大 中 小]

manifest 描述package中所有的内容

xmlns:Android 包含命名空间声明

Package 程序包

application   全局默认属性,如标签、权限等

android:icon  应用程序图标

android:label 应用程序名

Activity  可理解为窗口类

android:name  默认启动的Activity

action    组件支持的Intent action

category  组建支持的Intent Category。这里指定了应用程序默认启动Activity

uses-sdk   所用的SDK版本

点击main布局文件

通过拖拽我们可以在上面增加几个空间,当然也可以直接用代码生成

此时main中的代码为

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12. <EditText  
  13.  android:id="@+id/editText1"   
  14.  android:layout_width="match_parent"   
  15.  android:layout_height="wrap_content"   
  16.  android:inputType="number">  
  17.    <requestFocus></requestFocus>  
  18. </EditText>  
  19. <EditText  
  20.  android:id="@+id/editText2"   
  21.  android:layout_width="match_parent"   
  22.  android:layout_height="wrap_content"  
  23.   android:inputType="number"></EditText>  
  24. <Button   
  25. android:text="计算"  
  26.  android:id="@+id/button1"  
  27.   android:layout_width="wrap_content"   
  28.   android:layout_height="wrap_content"></Button>  
  29. </LinearLayout>  

android:id="@+id/editText2"

每个控件都该有自己的ID这样方便我们调用他

android:layout_width="match_parent" android:layout_height="wrap_content"

我们定义它的宽高

android:inputType="number">

让输入时只能输入数字

android:text="计算"

设置button显示的文字


现在我们可以进入系统更为我们建立的Activity中

  1. package bnu.ep;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.Toast;  
  9.   
  10. public class AdditiveActivity extends Activity {  
  11.     //输入控件   
  12.     private EditText et1,et2;  
  13.     //按钮   
  14.     private Button bt1;  
  15.     private int number1,number2,number3;  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         //让控件和布局文件中(main)的控件对应   
  20.         et1 = (EditText)findViewById(R.id.editText1);  
  21.         et2 = (EditText)findViewById(R.id.editText2);  
  22.         bt1 = (Button)findViewById(R.id.button1);  
  23.        //监听按钮点击事件   
  24.         bt1.setOnClickListener(new View.OnClickListener(){  
  25.             public void onClick(View arg0) {  
  26.                 //从EditText获取文本并转换成数字   
  27.                  String str1 = et1.getText().toString();  
  28.                    number1 = Integer.parseInt(str1);  
  29.                    String str2 = et2.getText().toString();  
  30.                    number2 = Integer.parseInt(str2);  
  31.                    number3 = number2 + number1;   
  32.                    Dostop(number3);  
  33.             }             
  34.         });          
  35.           
  36.     }  
  37.     private void Dostop(int number){  
  38.         //显示结果显示时间为8秒   
  39.         Toast.makeText(this,""+number, 8).show();  
  40.           
  41.     }  
  42.       
  43. }   

setContentView(R.layout.main);

这一句告诉我们需要显示的是哪个布局文件,也就是说我们可以自己建立其他的布局文件在这里显示,这里要注意自己建立的布局文件在命名时不可以用大写的英文。

让我们看一下结果吧

暂时用真机做的实验,关于模拟器的调试大家百度吧

linux
相关资讯       Android开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款