创建android实例应用
搭建好android环境后我们就可以创建android实例应用了,我们将在下一节中对这个实例用robotium进行测试
我们的这个应用是一个简单的乘法calculator,有两个输入,点击'Multiply'后将得到它们的乘法值
1、 创建project
点击File菜单,选择New,然后点击Others,然后如下图选择Android Project后,点击Next;
输入Project Name,点击Next;
点击Finish完成Project的创建;
另外,将上图中的Create a Test Project勾选上,将自动创建一个Test Project;这里我们可以将其勾上,因为后面还将用到Test Project;
创建成功后展开的图如下如示:
1、展开src目录可以看到com.calculator包,下面的Main.java包含了应用的整体架构; 2、在res目录我们可以定义应用的UI界面; 3、main.xml可以控制应用的界面; 4、string.xml可以定义在UI界面上可见的string值; 当然,我们不会深入去研究~ |
4、设计Layout层
在main.xml中输入如下代码并保存
<?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"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtSpace"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtFirstNumber"
/>
<EditText
android:inputType="numberDecimal"
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtSpace"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtSecondNumber"
/>
<EditText
android:inputType="numberDecimal"
android:id="@+id/EditText02"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtSpace"
/>
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtSpace"
/>
<Button
android:text="Multiply"
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
在string.xml中输入如下代码并保存
<?xml version="1.0"encoding="utf-8"?>
<resources>
<string name="hello">Enter two values and click on Calculate to multiply them.</string>
<string name="app_name">AndroidSimpleCalculator</string>
<string name="txtFirstNumber">Enter First Number</string>
<string name="txtSecondNumber">Enter Second Number</string>
<string name="txtSpace"></string>
</resources>
注意:这边如果刚开始只是先输入main.xml的话会出现error: Error: No resource found that matches the given name (at 'text' with value '@string/txtFirstNumber').
因为string.xml中还没对text定义所以会报错,不用管它,
把上面string.xml代码输进去后问题自然就没有了。
5、设计应用逻辑
在Main.java中输入如下代码并保存
packagecom.calculator;
importcom.calculator.R;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.TextView;
importandroid.text.Editable;
public classMainextendsActivity {
EditText FirstValue;
EditText SecondValue;
TextView Result;
Button Calculate;
floatnum1 , num2;
@Override
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FirstValue = (EditText) findViewById(R.id.EditText01);
SecondValue = (EditText) findViewById(R.id.EditText02);
Result = (TextView) findViewById(R.id.TextView01);
Result.setText("0.00");
Calculate = (Button) findViewById(R.id.Button01);
//Adding listener to button
Calculate.setOnClickListener(newView.OnClickListener() {
public voidonClick(View v) {
//Getting first & second values and passing to show result
showResult(FirstValue.getText(), SecondValue.getText());
}
});
}
//Showing multiply results
protected voidshowResult(Editable first, Editable second)
{
floatnum1 = Float.parseFloat(first.toString());
floatnum2 = Float.parseFloat(second.toString());
floatresult = num1 * num2;
Result.setText(String.valueOf(result));
}
}
应用已经设计好了,现在可以运行它了;
右击Project选择Run As后选择Android Aplicaton;
如果有正在运行的模拟器的话就选择正在运行的模拟器;
简单的calculator就创建好,下一步我们将创建AndroidCaculatorTest project来测试这个简易的calculator。