zoukankan      html  css  js  c++  java
  • Android Activity之间通信

    package com.example.myapp;
    
    import android.app.Activity;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MyActivity extends Activity {
    
        private Button btnOk = null;
    
        /**
         * Called when the activity is first created.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            btnOk = (Button)findViewById(R.id.btnCallOther);  //已学安卓两天学到碎片和活动之间通讯 布局,UI和空间 每天都很充实,  findViewById 要牢记 很常用 返回的是一个View对象 强制转换成需要的控线
            btnOk.setOnClickListener(new MyButtonListener());  //  监听器listener 安卓里很重要的一个机制 和adapter一样 很多动作都需要监听器来实现
    
        }
    
        class MyButtonListener implements View.OnClickListener {
    
    
            @Override
            public void onClick(View view) {//重写onClick方法
                Intent intent = new Intent(); // Intent 实现在活动之间转行 。2. 再活动之间传递消息
    
                intent.putExtra("key","value");   //putExtra 把一个程序的活动或者信息传递到下个活动
    
                intent.setClass(MyActivity.this,otherActivity.class);
    
                MyActivity.this.startActivity(intent);
            }
        }
    }
    
    
    package com.example.myapp;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    
    /**
     * Created by chang on 14-9-17.
     */
    public class otherActivity extends Activity{
        private TextView tv = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.other);
    
            Intent intent = getIntent();
    
            String s = intent.getStringExtra("key");
    
            tv = (TextView)findViewById(R.id.otherTextView);
            tv.setText(s);
        }
    }
     
  • 相关阅读:
    Floyd最小环
    D
    C
    B
    HPU personal training
    D
    A
    简练网软考知识点整理-项目风险审计及风险评估
    简练网软考知识点整理-项目需求跟踪及需求跟踪矩阵
    简练网软考知识点整理-项目资源优化、资源平衡及资源平滑
  • 原文地址:https://www.cnblogs.com/AceIsSunshineRain/p/5079391.html
Copyright © 2011-2022 走看看