android的Hello word

本文深入探讨了Android应用开发中Activity的概念,包括其创建要点、核心方法使用、Intent传递数据及新Activity注册流程,旨在帮助开发者更好地掌握Activity在Android应用开发中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是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传递数据不一定在同一个应用程序中——例如发送短信

转载于:https://www.cnblogs.com/hbiao68/archive/2011/12/21/2296221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值