android的Hello word

什么是Activity?
可以简单的理解Activity是一个控件容器,类似于windows的窗体

 

创建Activity的要点:
1、一个Activity就是一个类,并且这个类要继承Activity
2、需要复写onCreate方法(Activity第一运行的时候执行onCreate方法)
3、每一个Activity都需要在AndroidManifest.xml中注册
4、在layout目录下的文件中为Activity添加必要的控件

 

public void onCreate(Bundle bundle){
	super.onCreate(bundle);
	//指明该Activity使用的布局文件
	setContentView(R.layout.main);
	
	TextView myTextView = (TextView)findViewById(R.id.myTextView);
	Button myButton = (Button)findViewById(R.id.myButton);
	myTextView.setText("我的第一个TextView");
	myButton.setText("我的第一个Button");
}

<TextView
	//添加了id之后IDE就会自动的在R这个类中添加对应的ID
	android:id="@+id/myTextView"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
/>
<Button
	android:id="@+id/myButton"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
/>

 

例子:使用Intent的方法

Button myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new MyButtonListener());

//添加监听器——使用内部类
class MyButtonListener implements OnClickListener{
	public void onClick(View v){
		//生成一个Intent对象
		Intent intent = new Intent();
		//Activity02表示当前Activity类
		intent.setClass(Activity02.this, OtherActivity.class);
		Activity02.this.startActivity(intent);
	}
}

 
//要在AndroidManifest.xml文件中注册新添加的Activity文件

<activity android:name=".OtherActivity" android:label="@string/other"/>

备注: @sting/hello ---- 值R类当中的string类中的hello的值

 

Intent传递数据不一定在同一个应用程序中——例如发送短信

posted @ 2011-12-21 19:33  胖鹅  阅读(264)  评论(0编辑  收藏  举报