官方教程[Getting Started]

From http://developer.android.youdaxue.com/training/index.html
详情是英文记载,这里只贴出核心代码和笔记摘要。

1. Building Your First App

  1. 使用 AS或SDK,建立安卓项目
  2. 使用 AVD或真机,运行App
  3. 使用 TextView 和 Button 以及 LinearLayout ,建立UI
  4. 使用 Intent 来处理 按钮的 onClick 函数,启动第二个Activity
    同时,在第二个Activity中接收intent中的Extra,并动态生成TextView,添加到Layout中显示。

按钮处理,发送Intent

    public void sendMessage(View view) {
        // Do Something in response to button
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

接收的Activity's onCreate():

    protected void onCreate(Bundle savedInstanceState) {
        ...
        ...
        Intent intent = getIntent();
        String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        RelativeLayout layout = (RelativeLayout)findViewById(R.id.content);
        layout.addView(textView);
    }

2. Supporting Different Devices

  1. 适配不同的 语言
  2. 适配不同的 屏幕尺寸
  3. 适配不同的 版本平台

适配不同的语言,只需要新建出values文件夹,加上ISO语言后缀。

MyProject/
    res/
       values/
           strings.xml
       values-es/
           strings.xml
       values-fr/
           strings.xml

适配不同的屏幕尺寸和屏幕方向,如下:

MyProject/
    res/
        layout/              # default (portrait)
            main.xml
        layout-land/         # landscape
            main.xml
        layout-large/        # large (portrait)
            main.xml
        layout-large-land/   # large landscape
            main.xml

适配不同的分辨率(dpi):

MyProject/
    res/
        drawable-xhdpi/
            awesomeimage.png
        drawable-hdpi/
            awesomeimage.png
        drawable-mdpi/
            awesomeimage.png
        drawable-ldpi/
            awesomeimage.png

3. Managing the Activity Lifecycle

  1. 启动一个Activity
  2. 暂停和继续一个Activity
  3. 停止和重新启动一个Activity
  4. 重新创建一个Activity

每当一个Activity创建实例时,就会伴随着一系列与生命周期相关的回调函数来处理各个生命周期。

官方教程[Getting Started]_第1张图片
Basic Lifecycle

状态说明:

  • Resumed
    In this state, the activity is in the foreground and the user can interact with it. (Also sometimes referred to as the "running" state.)
  • Paused
    In this state, the activity is partially obscured by another activity—the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does not receive user input and cannot execute any code.
  • Stopped
    In this state, the activity is completely hidden and not visible to the user; it is considered to be in the background. While stopped, the activity instance and all its state information such as member variables is retained, but it cannot execute any code.

这三个状态都是属于“稳态”其他的都是属于过度状态。因此只要分别处理好onCreate()、onStart()、和onResume()即可。

设定App的第一启动Activity

编辑位于项目根目录的 AndroidManifest.xml,加上来指明:


    
        
        
    

** 注意 **
如果没有任何Activity同时指出了 MAIN的action 和LAUNCHER的category,那么这个app就不会出现在手机主屏幕的APP列表中。

结束一个Activty

系统会在onPause()和onStop()方法执行后,才调用onDestroy()。不过,如果使用__ finish() __方法,那么将直接调用onDestroy()来结束Activity的生命周期。

每个周期回调函数内该做的,各种保存app状态的详细代码。请参考原始文档。

你可能感兴趣的:(官方教程[Getting Started])