Android的第一个入门简单例子

Summary steps to develop an application:
  1. Install Android SDK
  2. Install ADT Eclipse Plugin
  3. Create an Android Virtual Device(AVD)
  4. Create Android Project with Eclipse
  5. Code it.
  6. Start it in Android AVD

Tools Used it this tutorial
1. JDK 1.6
2. Eclipse IDE
3.Android SDK


代码

public class HelloWorldActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_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;
	}

}



直接运行就可以看到helloWorld。这个项目不需要编写任何代码,新建Project就是HelloWorld了。那么为什么呢?
主要代码就是setContentView(R.layout.activity_hello_world);
其实helloWorld是从strings.xml中配置的。下面看看 res/strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">HelloWorld</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
</resources>

页面显示的HelloWorld就是显示了<string name="hello_world">Hello world!</string>中的值。你可以通过修改这个值来变化其显示的值。

这个例子很简单,只需要明白一点那就是在strings.xml中可以配置一些值。
下一个例子主要看看Activity

猜你喜欢

转载自jiangduxi.iteye.com/blog/2006995