Activity小品
Activity小品
一、程序效果
1.程序窗口中有一个按钮Button和TextView,我们可以通过写过代码修改该Button和TextView上便签
二、代码编写和解析
1.在HelloWorld程序(Android版HelloWorld附件)基础上
2.编写layout包下的main.xml文件,主要是添加Button控件和为控件指定id
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" > <!-- android:id 为控件指定id 使用:@+id/控件名 android:layout_width 指定控件的宽 android:layout_height 指定控件的长 fill_parent:填充父控件的宽度,高度,即宽度,高度父控件一样 wrap_content:利用所包裹的内容填充,即内容多大控件多大 --><TextView android:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /><Buttonandroid:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content"/></LinearLayout>?
3.编写自动生成的Activity类,主要是通过id取得控件并对控件进行一些操作
?
?
package linys.activity;import android.app.Activity;import android.os.Bundle;import android.widget.Button;import android.widget.TextView;public class ActivityDemoActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //根据id取得控件 TextView textView=(TextView)findViewById(R.id.myTextView); Button button=(Button)findViewById(R.id.myButton); //对控件上的便签进行修改 textView.setText("textView"); button.setText("button"+"\n"+"text");//wrap_content效果 }}?三、程序解析
1.Activity可以看作是一个控制器,控制着窗体的布局的加载以及窗体上控件的修改,和界面与业务程序间的调用
??????? 2.创建Activity类时注意的:
??????????? 1)继承Activity
??????????? 2)重写OnCreate方法
??????????? 3)在AndroidMainfest.xml中配置
?
?