首先安装android SDK;PATH配置引入tools;即可使用android命令
1.创建工程
android create project -n HelloWorld -t 1 -p HelloWorld -k org.crazyit.helloworld -a HelloWorld
-n 工程名
-t 工程对应的安卓平台ID,可用android list targets查看对应ID
-p 工程保存路径
-k 给工程报名
-a 制定activity名称
该命令将会在当前路径下创建hello world文件夹,文件夹中即为所创建工程;
res下存放各种资源,如layout存放界面布局文件;
src下存放Java源码;
build.xml为ant生成文件;
2.开发自己的应用:布局用户界面
android:id="@+id/show" 标识
修改./HelloWorld/res/layout/main.xml,设计用户应用界面
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/show"
android:layout_centerInParent="true"
android:onClick="clickHandler"
android:text="@string/click" />
</LinearLayout>
界面预期结果:
3. 开发自己的应用:补充Java代码
进入./HelloWorld/src/org/crazyit/helloworld/HelloWorld.java,添加动作
package org.crazyit.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class HelloWorld extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello_world, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//添加动作
public void clickHandler(View source){
TextView tv = (TextView) findViewById(R.id.show);
tv.setText("hello android!\n" + new java.util.Date());
}
}
4. 把工程运行起来 go!
使用ant将工程打包运行 ant install
clean
debug
release
test
install
uninstall
PS:res中使用了资源@string,修改./HelloWorld/res/values/string.xml添加对应关系
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello World !</string>
<string name="click">点击</string>
</resources>
工程清单文件AndroidManifest.xml