码迷,mamicode.com
首页 > 其他好文 > 详细

01_Activity生命周期及传递数据

时间:2014-10-20 21:02:18      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   os   ar   sp   数据   

1. Activity的生命周期:

bubuko.com,布布扣

2. Activity启动另一个Activity,并传递数据:

package com.example.activitydemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    protected static final String EXTRA_MESSAGE = "com.example.activitydemo.MESSAGE";
    public Button btnActivityChange;
    public EditText editMessage;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnActivityChange = (Button) findViewById(R.id.btn1);
        editMessage = (EditText) findViewById(R.id.editMessage);

        btnActivityChange.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, Activity2.class);
                
                String message = editMessage.getText().toString();
                i.putExtra(EXTRA_MESSAGE, message);
                
                startActivity(i);
            }
        });

    }

}
package com.example.activitydemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Activity2 extends Activity {

    private Button btnBack;
    private TextView textView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);

        btnBack = (Button) findViewById(R.id.btnBack);
        textView1 = (TextView) findViewById(R.id.textView1);

        Intent intent = getIntent();
        textView1.setText(intent.getStringExtra(MainActivity.EXTRA_MESSAGE));

        btnBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

}

 

01_Activity生命周期及传递数据

标签:android   style   blog   http   color   os   ar   sp   数据   

原文地址:http://www.cnblogs.com/510602159-Yano/p/4038389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!