android开发入门-1(使用意图传递数据)
2013-06-18 22:36
666 查看
青春是用来挥霍的,但也是要学东西的。学会多种技能,以后的日子才不会被饿晕。android开发,比较容易入门,学学这个还比较好玩。
其他乱起八糟的就不多说了。直接入正题吧。
使用意图传递数据之通用方式。
测试应用:当前页面点击button传递数据到一个新的页面显示在textview中。
首先在,mainActivity.xml文件中加入一个button按钮
其他乱起八糟的就不多说了。直接入正题吧。
使用意图传递数据之通用方式。
测试应用:当前页面点击button传递数据到一个新的页面显示在textview中。
首先在,mainActivity.xml文件中加入一个button按钮
<Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="测试使用Intent传递数据" > </Button>然后在,MainActivity.java中加入
private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//加载布局文件 button=(Button)this.findViewById(R.id.button);//获取button button.setOnClickListener(new View.OnClickListener() {//点击button事件 @Override public void onClick(View v) { // TODO Auto-generated method stub //创建意图 Intent intent=new Intent(Main.this, OtherActivity.class); //在意图中传递数据 intent.putExtra("name", "张三"); intent.putExtra("age", 21); intent.putExtra("address", "北京"); //启动意图 startActivity(intent); } }); // Log.i(TAG, "onCreate methos is executed"); }在布局文件中,新建一个other.xml文件。
<TextView android:id="@+id/msg" android:layout_width="fill_parent" android:layout_height="fill_parent" > </TextView>然后新建一个OtherActivity.class 并加入
private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); textView = (TextView)this.findViewById(R.id.msg);//获取文本框 Intent intent = getIntent();//获取意图对象 Integer age = intent.getIntExtra("age", 0);//获取意图对象数据 String name = intent.getStringExtra("name"); //获取意图对象数据 String address = intent.getStringExtra("address");//获取意图对象数据 textView.setText("age--->>"+age+"\n"+"name--->>"+name+"\n"+"address--->>"+address);//设置到文本框 }最后不要忘记在清单文件中加入OtherActivity。
<activity android:name=".OtherActivity"></activity>
相关文章推荐
- Android入门篇六:使用意图传递数据之返回结果
- android开发入门-2(使用静态变量传递数据)
- Android入门篇二:使用意图在Activity之间传递数据
- android开发入门-4(使用全局变量传递数据)
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- android之 在Activity之间使用意图传递数据(使用静态变量传递数据)
- Android 开发中使用Intent传递数据的方法
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- android使用意图传递数据之返回结果
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- Android使用意图传递数据的四种方式
- android 开发使用剪切板传递数据
- Android 开发中使用Intent传递数据的方法
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- Android入门篇三:使用静态变量在Activity之间传递数据
- Android开发从入门到放弃(1)在Activity之间传递数据
- Android开发:使用序列化接口Parcelable、Serializable实现Activity间传递复杂数据类型参数
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- 【Android 开发教程】使用Intent传递数据
- Android使用意图传递数据的几种方式