分类目录

展开|收起

看你喜欢

(1) (1) (42) (1) (1) (1) (16) (2) (1) (1) (4) (1) (2) (7) (4) (1) (1) (1) (1) (3) (1) (5) (1) (1) (1) (1) (1) (2) (1) (4) (4) (3) (1) (1) (2) (1) (37) (2) (1) (5) (3) (1) (4) (1) (1) (11) (3) (1) (9) (3) (1) (23) (2) (1) (2) (1) (1) (1) (1)

最新精华

《Android应用开发详解》- Chapter05_Activity_TransData – Activity之间传递数据

在WEB开发中,我们常把数据放在某个Scope中(如request、session)来实现数据的共享,在Android系统中不同的Activity之间是如何传递数据的呢?这里要用到另一个对象Bundle,我们将要传递的信息封装到该对象里面,并通过Intent对象传递到另一个Intent中。

1. 布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="horizontal" android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		
		<TextView 
		android:text="请输入用户名称:" 
		android:id="@+id/TextView01"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"></TextView>
			
		<EditText 
		android:text="" 
		android:id="@+id/EditText01"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content"></EditText>
	</LinearLayout>
	
	<Button 
	android:text="查询" 
	android:id="@+id/Button01"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content"
	android:layout_gravity="right"></Button>
		
</LinearLayout>

2. 布局文件result.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
    <TextView 
    android:text="" 
    android:id="@+id/TextView02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></TextView>

</LinearLayout>

3. MainActivity.java

package com.amaker.test;

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;
import android.widget.EditText;

public class MainActivity extends Activity {
	private Button b1;
	private EditText myEditText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         b1 = (Button)findViewById(R.id.Button01);
         
         myEditText = (EditText)findViewById(R.id.EditText01);
         b1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				 String name = myEditText.getText().toString();
		         Bundle data = new Bundle();
		         data.putString("name", name);
		         
		         Intent intent = new Intent(MainActivity.this,ResultActivity.class);
		         intent.putExtras(data);
		         startActivity(intent);
			}
		});
         
    }
}

4. ResultActivity.java

package com.amaker.test;

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

public class ResultActivity extends Activity {
	private TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		setContentView(R.layout.result);
        tv = (TextView)findViewById(R.id.TextView02);
		
		Intent intent = getIntent();
		Bundle b = intent.getExtras();
		String name = b.getString("name");

		String[] projection = new String[] { Contacts._ID,
				Contacts.DISPLAY_NAME};

		Uri contacts = Contacts.CONTENT_URI;
		String[] args = {name};
		
		Cursor managedCursor = managedQuery(contacts, projection, 																		
				"display_name=?", 
				args, 
				Contacts.DISPLAY_NAME + " ASC");
		
		
		if(managedCursor.moveToFirst()){
			Uri contacts_phone = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;			
			String[] phone  = new String[] { ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
					ContactsContract.CommonDataKinds.Phone.NUMBER};
			
			String ID = managedCursor.getString(0);
			String[] args1 = {ID};		
			Cursor managedCursor_phone = managedQuery(contacts_phone, phone, 																		
					"contact_id=?", 
					args1, 
					null);
			
			String number = ""; 			
			if(managedCursor_phone.moveToFirst()){
				number = managedCursor_phone.getString(1);				
			}
			
			String name1 = managedCursor.getString(1);
			tv.setText(name1+":"+number);
		}

	}
}

注:我对原书程序略作了修改,因为提取联系人号码现在的版本不能直接用Peopple.NUMBER了,要先获取到联系人ID,再用ID查ContactsContract.CommonDataKinds.Phone.NUMBER来获取。

5. 运行结果

Chapter05_Activity_TransData-1

Chapter05_Activity_TransData-2

Chapter05_Activity_TransData-3

Chapter05_Activity_TransData-4

Chapter05_Activity_TransData实例源码:

Chapter05_Activity_TransData
标题: Chapter05_Activity_TransData (928 次点击)
文件: Chapter05_Activity_TransData.7z
大小: 20 kB
描述:

  打分:5.0/5 (共2人投票)
(浏览总计: 42 次)
Add Comment Register



发表回复

  

  

  

您可以使用这些HTML标签

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>