Activity与view

Activity的作用:一个Activity相当于一个容器,用于存放各种控件的容器,也可以理解为是与用户交互的接口

创建Activity的要点:
   1.一个Activity就是一个类,并且这个类要继承Activity
   2.需要复写onCreate方法
   3。设置这个Activity所使用的布局文件(一般一个Activity对应一个布局文件,也可以多个Activity对应一个布局文件)
   4.每一个Activity都需要在AndroidManifest.xml文件当中进行注册
   5.在布局文件中为Activity添加必要的控件
   6.可以在Activity.java文件中通过控件的id找到需要的控件

Activity启动的基本流程:

  1.操作系统读取AndroidManifest.xml文件决定要运行哪个Activity,
  2.找到Activity之后操作系统自动生成Activity对象,
  3.系统自动调用Oncreate方法,在这个方法中读取指定的布局文件,显示布局文件里设置的内容


View:所有控件的父类,文本类,按钮类,布局,等等。。。。

 1 import android.app.Activity;
 2 import android.graphics.Color;
 3 import android.os.Bundle;
 4 import android.view.View;
 5 import android.view.View.OnClickListener;
 6 import android.widget.Button;
 7 import android.widget.TextView;
 8 
 9 public class MainActivity extends Activity {
10     //声明控件
11     private TextView testView;
12     private Button button;
13     int count = 0;
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         //设置Activity使用的布局文件
17         setContentView(R.layout.activity_main);
18         //findViewById方法返回的View,所以要向下转型
19         testView = (TextView) findViewById(R.id.testView);
20         //这里设置控件属性和在布局文件设置效果是一样的,如果有同样的属性结果是显示这里设置的为准
21         testView.setText("huangjianfeng");
22         //设置这个控件的背景颜色
23         testView.setBackgroundColor(Color.BLACK);
24         
25         button = (Button) findViewById(R.id.button);
26         button.setBackgroundColor(Color.BLUE);
27         ButtonListener buttonListener=new ButtonListener();
28         button.setOnClickListener(buttonListener);
29     }
30     
31     class ButtonListener implements OnClickListener{
32 
33         public void onClick(View v) {
34             count++;
35             testView.setText(count+"");
36         }
37         
38     }
39 
40 }

 布局文件:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context=".MainActivity" >
11 
12     <TextView
13         android:id="@+id/testView"
14         android:layout_width="fill_parent"
15         android:layout_height="wrap_content"
16         android:background="#FF0000"
17         android:text="0" />
18     
19     
20     <Button
21         android:id="@+id/button"
22         android:layout_width="fill_parent"
23         android:layout_height="wrap_content"
24         android:background="#FFFF00"
25         android:text="0"
26         />
27 
28 </LinearLayout>
29 <!--TextView:文本控件,
30     Button:按钮控件
31     match_parent:表示覆盖父控件-->

 清单文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.mars.viewdemo"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="18" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.mars.viewdemo.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25     </application>
26 
27 </manifest>

 string.xml

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

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

</resources>

 

posted @ 2014-03-06 13:52  胡椒粉hjf  阅读(181)  评论(0编辑  收藏  举报