Android 零基础学习Activity生命周期

Activity 

Activity其实就相当于CSS样式,Activity实际上就是表示的是一个人机的交互程序,用于存放各个显示控件,也是Android的基本组成,所有的Android项目都使用Java语言进行开发,所以每一个继承了android.app.Activity的Java类都将成为一个Activity程序,而一个Android项目将由多个Activity程序所组成,而所有的显示组件都必须放在Activity上才可以进行显示,android.app.Activity类的继承结构如下:
java.lang.Object
       ↳android.content.Context
            ↳android.content.ContextWrapper
                ↳android.view.ContextThemeWrapper
                      ↳android.app.Activity 


Activity类的常用方法


Android项目中的文件夹作用


Android项目中的文件作用 


开发Android程序

打开文件res/layout/activity_main.xml(以后系统都默认打开的是这个文件)

[html]  view plain  copy
 print ?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <TextView  
  7.         android:id="@+id/textView1"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello_world" />  
  11.   
  12.     <TextView  
  13.         android:id="@+id/mytext"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.          />  
  17.   
  18.     <Button  
  19.         android:id="@+id/mybut"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content" />  
  22. </LinearLayout>  


扫描二维码关注公众号,回复: 1979956 查看本文章


定义布局管理器,并增加组件

打开默认创建的文件:src/com.example.firstgingerbread/MainActivity.java

[java]  view plain  copy
 print ?
  1. package com.example.firstgingerbread;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.widget.Button;  
  7. import android.widget.LinearLayout;  
  8. import android.widget.TextView;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);                    //调用父类的onCreate()方法  
  15.         LinearLayout layout = new LinearLayout(this);    //定义布局管理器  
  16.         layout.setOrientation(LinearLayout.VERTICAL);    //垂直 摆放所有组建  
  17.         TextView text = new TextView(this);                //创建文本显示组建  
  18.         text.setText(super.getString(R.string.message));        //从资源文件中设置文字  
  19.         Button but = new Button(this);  
  20.         but.setText(R.string.buttonstring);  
  21.         layout.addView(text);  
  22.         layout.addView(but);  
  23.         super.setContentView(layout);          
  24.     }  
  25.   
  26.     @Override  
  27.     public boolean onCreateOptionsMenu(Menu menu) {  
  28.         // Inflate the menu; this adds items to the action bar if it is present.  
  29.         getMenuInflater().inflate(R.menu.main, menu);  
  30.         return true;  
  31.     }  
  32.   
  33. }  



编辑values\strings.xml文件 

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">firstGingerbread</string>  
  5.     <string name="action_settings">Settings</string>  
  6.     <string name="hello_world">Hello world!</string>  
  7.     <string name="message">Welcom to my world!</string>  
  8.     <string name="buttonstring">按钮</string>  
  9. </resources>  



小结

Android项目由若干个Activity程序所组成,每一个Activity都是一个Java类; 
一个Android项目中所有用到的资源都保存在res文件夹之中; 
Android中的组件需要在布局管理器中进行配置,之后在Activity程序中可以使用findViewById()方法查找并进行控制; 
在布局管理器中定义的每一个组件都有其对应的操作类,用户可以直接实例化这些类的对象进行组件的定义显示; 
标准的Android项目,所有的文字显示信息应该保存在strings.xml文件中保存。 

猜你喜欢

转载自blog.csdn.net/weixin_41615115/article/details/79220219