您的位置:首页 > 移动开发 > Android开发

Android Activity之间通信

2015-12-27 00:51 337 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: