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);
        }
    }
     
  • 相关阅读:
    NOIP2018 游记
    HDU1556 敌兵布阵
    BZOJ 1032 [JSOI2007]祖码Zuma
    BZOJ 1068 [SCOI2007]压缩
    BZOJ 1090 [SCOI2003]字符串折叠
    BZOJ 1260 [CQOI2007]涂色paint
    BZOJ 1055 [HAOI2008]玩具取名
    HDU 5151 Sit sit sit
    HDU 4283 You Are the One
    vue系列8:webpack
  • 原文地址:https://www.cnblogs.com/AceIsSunshineRain/p/5079391.html
Copyright © 2011-2022 走看看