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中的代码为
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <EditText
- android:id="@+id/editText1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="number">
- <requestFocus></requestFocus>
- </EditText>
- <EditText
- android:id="@+id/editText2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="number"></EditText>
- <Button
- android:text="计算"
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></Button>
- </LinearLayout>
android:id="@+id/editText2"
每个控件都该有自己的ID这样方便我们调用他
android:layout_width="match_parent" android:layout_height="wrap_content"
我们定义它的宽高
android:inputType="number">
让输入时只能输入数字
android:text="计算"
设置button显示的文字
现在我们可以进入系统更为我们建立的Activity中
- package bnu.ep;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class AdditiveActivity extends Activity {
- //输入控件
- private EditText et1,et2;
- //按钮
- private Button bt1;
- private int number1,number2,number3;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //让控件和布局文件中(main)的控件对应
- et1 = (EditText)findViewById(R.id.editText1);
- et2 = (EditText)findViewById(R.id.editText2);
- bt1 = (Button)findViewById(R.id.button1);
- //监听按钮点击事件
- bt1.setOnClickListener(new View.OnClickListener(){
- public void onClick(View arg0) {
- //从EditText获取文本并转换成数字
- String str1 = et1.getText().toString();
- number1 = Integer.parseInt(str1);
- String str2 = et2.getText().toString();
- number2 = Integer.parseInt(str2);
- number3 = number2 + number1;
- Dostop(number3);
- }
- });
- }
- private void Dostop(int number){
- //显示结果显示时间为8秒
- Toast.makeText(this,""+number, 8).show();
- }
- }
setContentView(R.layout.main);
这一句告诉我们需要显示的是哪个布局文件,也就是说我们可以自己建立其他的布局文件在这里显示,这里要注意自己建立的布局文件在命名时不可以用大写的英文。
让我们看一下结果吧


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