码迷,mamicode.com
首页 > 移动开发 > 详细

【Android开发-9】信息时代,Activity和Activity怎么交流

时间:2014-08-26 19:42:46      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:android   style   http   color   os   java   io   strong   for   

前言:在信息时代,人们不必像古代人那样飞鸽传书,随便一个短信、微信、QQ、微博都可以和自己亲爱的小伙伴快速沟通交流。在这样眼花缭乱的信息时代,选择一种合适自己的沟通工具是很有必要的,Android中的Activity与Activity之间的传参方法也是很多种的,在项目中怎么选择信息交互方法,就看项目的需求和自己的经验。


弄例子之前需要了解一个东西:Intent;

书上和网上看到的概念解释都很多,但本人喜欢它的英文意思:意图。你的意图想干嘛,跟它说了就行。比如我要传参,那你在这个Intent中设置完了,到第二个界面通过你设置的意图传参,就可以解析出来。比如你意图要打开界面,你在Intent里面设置好要打开的界面参数,然后通过方法调用就可以。或者更简单的说明就是起传递作用!


开始实战理解:

1.新建个项目:ActivityIntent


2.根据上一篇,创另外一个界面,创建的文件为:activity_first.xml和FirstActivity


activity_first.xml中界面布局的代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <EditText
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:layout_centerHorizontal="true" />
    
    <Button 
        android:id="@+id/close_1"
        android:layout_below="@id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="关闭"
        
        />

</RelativeLayout>


主界面布局文件代码如下:

<RelativeLayout 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="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="界面之间传参"
        android:layout_centerHorizontal="true" />
    <Button 
        android:id="@+id/Open_1"
        android:layout_below="@id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Intent传参(第一种)"
        
        />
      <Button 
        android:id="@+id/Open_2"
        android:layout_below="@id/Open_1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Intent传参(第二种)"
        
        />
        <Button 
        android:id="@+id/Open_3"
        android:layout_below="@id/Open_2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Intent传参(第三种)"
        
        />

</RelativeLayout>


3.把新建的界面布局加入程序,修改下AndroidManifest.xml,在</application>节点前添加内容如下:

 <activity
            android:name=".FirstActivity"
            android:label="@string/app_name" >
 </activity>

4.编写入口代码MainActivity如下:

package com.wyz.activityintent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button intentBtn = (Button)findViewById(R.id.Open_1);
		Button intentBtn_2 = (Button)findViewById(R.id.Open_2);
		Button intentBtn_3 = (Button)findViewById(R.id.Open_3);
		
		intentBtn.setOnClickListener(new BtnListener());
		intentBtn_2.setOnClickListener(new BtnListener());
		intentBtn_3.setOnClickListener(new BtnListener());
	}
	
	class BtnListener implements OnClickListener {

		@Override
		public void onClick(View v) {
		
			Intent intent = new Intent(MainActivity.this, FirstActivity.class);
			
			switch(v.getId())  
            {  
            	case R.id.Open_1:  //第一种
            	
            		intent.putExtra("name", "wyz_1_");
            		intent.putExtra("age", 18);
            		startActivity(intent);
            		break;
            		
            	case R.id.Open_2:  //第二种

            		Bundle bundle = new Bundle();
            		bundle.putString("name", "wyz_2_");
            		bundle.putInt("age", 27);
            	
            		intent.putExtras(bundle);
            		startActivity(intent);
            		break;
            	case R.id.Open_3: //第三种

            		intent.putExtra("name", "wyz_3_");
            		intent.putExtra("age", 35);
            
            		startActivityForResult(intent, 0);
            		
            		break;
            	default:
            			break;
            }
		}
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == 0)	//这个请求码对应startActivityForResult(intent, 0);
		{
			if(resultCode == 1)  //这个请求码对应打开界面的setResult(1, resultIntent);
			{
				String sRet = data.getStringExtra("return");  
				
				Toast.makeText(getApplicationContext(), sRet, Toast.LENGTH_LONG).show();;
         
			}
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}
}


4.编写另外一个界面业务代码FirstActivity如下:

package com.wyz.activityintent;

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

public class FirstActivity extends Activity {

	int iType=0;	//判断是第几种方法触发,默认0
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_first);
		
		EditText text = (EditText)findViewById(R.id.text);
		Button close_btn = (Button)findViewById(R.id.close_1);
		
		close_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(iType == 3)	//等于第三种时,关闭界面返回参数
				{
					Intent resultIntent = new Intent();  
					resultIntent.putExtra("return", "Hello wyz");  
					setResult(1, resultIntent);  
					FirstActivity.this.finish();  
				}
				else
				{
					FirstActivity.this.finish();
				}
				FirstActivity.this.finish();
			}
		});
		
		Intent intent = getIntent();
		
		String sName = intent.getStringExtra("name");
		
		//int iAge = intent.getIntExtra("age", 0);  //获取参数第一种
		int iAge = intent.getExtras().getInt("age");	//获取参数第二种
		String sAge = String.valueOf(iAge);
		text.setText(sName+sAge);	//设置显示结果
		
		
		if(sName.compareTo("wyz_3_") == 0)
		{
			iType = 3;
		}
		
	}
}


实战效果:

bubuko.com,布布扣







【Android开发-9】信息时代,Activity和Activity怎么交流

标签:android   style   http   color   os   java   io   strong   for   

原文地址:http://blog.csdn.net/qivan/article/details/38715421

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!