背景:
阅读新闻
Android在Activity之间传数据之Intent
[日期:2013-01-13] 来源:Linux社区 作者:luyee2010 [字体:]
Android在Activity之间传数据之Intent
FirstActivity.java:
package com.android.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new MyBt_OnClickListener());
}
class MyBt_OnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("intentKey", "intentValue");
intent.setClass(FirstActivity.this, SecondActivity.class);
FirstActivity.this.startActivity(intent);
}
}
}
对应的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:text="@string/first"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bt"/>
</LinearLayout>
---SecondActivity.java\
package com.android.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SecondActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.second_tv);
Log.e("intentKey", intent.getStringExtra("intentKey"));
tv.setText(intent.getStringExtra("intentKey"));
}
}
对应的xml:second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second"
android:id="@+id/second_tv"/>
</LinearLayout>
Hadoop源码分析之RPC(Remote Procedure Call Protocol)
相关资讯 Android Intent
本文评论 查看全部评论 (0)
尊重网上道德,遵守中华人民共和国的各项有关法律法规 承担一切因您的行为而直接或间接导致的民事或刑事法律责任 本站管理人员有权保留或删除其管辖留言中的任意内容 本站有权在网站内转载或引用您的评论 参与本评论即表明您已经阅读并接受上述条款
评论声明
最新资讯