Android学习笔记之携带数据跳转页面

    当一个Android运行后,需要经常的跳转页面(比如查看->返回),那么在跳转的同时也需要携带一些源页面的数据到目的页面。下面就举例说一下此类需求的实现方法

    首先创建源页面视图first.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".First" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText 
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入一个字符串"        
        />
    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="Btn"        
        />
</LinearLayout>
    创建源页面类first.class
package com.example.demo2;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class First extends Activity {
	private Button btn = null;
	private EditText et = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.first);
		//获取视图中资源
		btn = (Button)findViewById(R.id.btn);
		et = (EditText)findViewById(R.id.et);
		//为button添加监听事件(内部类)
		btn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String name = et.getText().toString();
				Bundle bundle = new Bundle();
				bundle.putString("name",name);
				Intent intent = new Intent(First.this,Second.class);
				intent.putExtras(bundle);
                               //别忘了启动intent 
                                startActivity(intent);
			}
		});
	}
}
    创建源页面视图second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".First" >

    <TextView
         android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

    创建源页面类first.class

package com.example.demo2;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.TextView;

public class Second extends Activity{
	private TextView tv= null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		
		tv = (TextView)findViewById(R.id.result);
		Intent intent = getIntent();
		Bundle b = intent.getExtras();
		String name = b.getString("name");
		tv.setText(name);
//		String[] projection = new String[] {People._ID,People.NAME,People.NUMBER};
//		Uri contacts = People.CONTENT_FILTER_URI;
//		String[] args = {name};
//		Cursor managedCursor = managedQuery(contacts, projection, "name=?", args, People.NAME+"ASC");
//		if(managedCursor.moveToFirst()) {
//			String name1 = managedCursor.getString(1);
//			
//			String number = managedCursor.getString(2);
//			tv.setText(name);
//		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值