Activity单向传值

Activity单向传值

1. 运行原理

1.两个页面之间进行交互,首先要创建Intent对象,Intent像是两个页面之间的桥梁,而Bundle像是集装箱,Android将要传送的数据放在Bundle中,通过intent提供的PutExtras()方法将要传送的数据传送过去。
2.Bundle相当于超市的储物柜,一个编号对应柜子,拿到编号就可以拿走物品,Bundle携带的数据类型可以是基本数据类型也可以是数组,还可以是对象,或者对象数组,如果是对象或者对象数组,需要使用Serialzable或者Pracelable接口。
注意:在使用Bundle传递数据的时候,Bundle是右大小限制的,数据必须小于0.5MB,如果大于这个值会报TransactionTooLargeException异常

2.设置一个主活动Main和两个副活动(Question和Answer)

3.1 ,Question的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="${relativePackage}.${activityClass}"
    android:orientation="vertical"
    android:gravity="center"   
     >
     	<TextView
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/title_translation"
	        android:layout_marginTop="20dp"
	        android:layout_marginBottom="20dp"
	        android:textSize="50dp"       
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"        
        >       
        <Button
            android:id="@+id/btn_true" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="正确"
            android:textSize="30dp"
            android:layout_marginRight="20dp"
            
            />
         <Button
            android:id="@+id/btn_false" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="错误"
            android:textSize="30dp"
            android:layout_marginLeft="20dp"
            
            />
    </LinearLayout>
    
    <Button
            android:id="@+id/btn_lookanswer" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查看答案"
            android:textSize="30dp"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            
            />
</LinearLayout>

效果:::
在这里插入图片描述

3.2,Question的Java文件代码

原理:创建一个全局变量EXTER_ANSWER,存储发送的值,利用intent启动AnswerActivity活动,并用intent.putExtra(变量name,value);方法把EXTER_ANSWER传入

public class QuestionActivity extends Activity {
	
	private Button lookanswer;
	
	//用来传值的参数
	public static final String EXTER_ANSWER="";


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_question);
		lookanswer = (Button) findViewById(R.id.btn_lookanswer);
		lookanswer.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(QuestionActivity.this,AnswerActivity.class);
				//单向传值,给intent附加一个值,这个值时问题的正确答案
				
				intent.putExtra(EXTER_ANSWER,"");
				
				startActivity(intent);
			}
		});
		
	}
}

4.1,Question的XML文件

<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}"
    android:gravity="center"
     >
    <!-- 居中:android:layout_width="match_parent"    android:gravity="center_horizontal"都不能少 -->
    <TextView
        android:id="@+id/btn_answer_true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="真的要查看答案吗?"
        android:layout_marginBottom="30dp"
        android:gravity="center_horizontal"
         />
    
            <!-- 隐藏::android:visibility="invisible"   invisible留出隐藏控件的空间 -->
    <TextView
        android:id="@+id/btn_lookanswer"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:text="正确:1+1=2"
         android:layout_marginBottom="30dp"
         android:layout_below="@id/btn_answer_true"
         android:gravity="center_horizontal"
         android:visibility="invisible"
        
        />
    <Button
        android:id="@+id/show_answer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击查看答案"
        android:layout_marginBottom="30dp"  
        android:layout_below="@id/btn_lookanswer" 
        android:gravity="center_horizontal"      
        />

</RelativeLayout>

效果:::

在这里插入图片描述

4.2,Question的java文件代码

原理:设置一个变量answer,用来接收值,用==getintent()==获取Intent对象,判断传来的intent是否为空,绑定组件,并判断做出应答,
lookanswer.setText(“正确”);-------在TextView里设置文本
lookanswer.setVisibility(1);--------设置TextView是否隐藏—与 android:visibility="invisible"相对应

public class AnswerActivity extends Activity {
	Boolean answer=false;
	private Button showanswer;
	private TextView lookanswer;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_answer);
		//不能重新new了,重新实例化变空
		Intent intent=getIntent();
		//注意不等于空的写法
		//如果前边传值过来了,进去判断,且得到值,
		if(intent!=null){	
			//intent.getBooleanExtra(参数名, 传不过来值-设置的默认值)
			answer=intent.getBooleanExtra(QuestionActivity.EXTER_ANSWER, true);
		}
		lookanswer=(TextView) findViewById(R.id.btn_lookanswer);
		showanswer=(Button) findViewById(R.id.show_answer);
		showanswer.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(answer==true){
					lookanswer.setText("正确");
				}else{
					lookanswer.setText("错误");
				}
				//1.显示。2.隐藏。3横线
				lookanswer.setVisibility(1);			
			}
		});		
	}
}

5,例2----在输入框中输入信息,并在另一个活动中拿到输入的信息

5.1,demo1Activity.xml

设置四个输入框,和一个按钮
    <TextView
        android:id="@+id/sh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学生基本信息表"
        android:textSize="30dp"
         />
    <EditText
        android:id="@+id/sh_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入学生姓名"
        android:textSize="25dp" 
        />

    <EditText
        android:id="@+id/sh_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入学生年龄"
        android:textSize="25dp" 
        />
    <EditText
        android:id="@+id/sh_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入学生性别"
        android:textSize="25dp" 
        />
    <EditText
        android:id="@+id/sh_yuan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入学生学院"
        android:textSize="25dp" 
        />
    
    <Button
        android:id="@+id/sh_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="点击产看详细信息"
        android:textSize="25dp" 
        />

效果:::在这里插入图片描述

5.2,demo1Activity.java

原理:通过绑定组件,用getText()获取到输入值,利用intent传递Bundle对象
Bundle,实现数据打包
Toast.makeText(Demo1Activity.this, “输入信息有误,请重新输入”, Toast.LENGTH_SHORT).show(); ----输出“输入信息有误,请重新输入”这句话

public class Demo1Activity extends Activity {
	
	private Button shok;
	EditText editText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_demo1);
		
		shok = (Button) findViewById(R.id.sh_ok);
		shok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			//获取文本框内容
			//.toString()--作用:把前边获取的内容变成String类型
			String Str1 = ((EditText)findViewById(R.id.sh_name)).getText().toString();
			String Str2 = ((EditText)findViewById(R.id.sh_age)).getText().toString();
			String Str3 = ((EditText)findViewById(R.id.sh_sex)).getText().toString();
			String Str4 = ((EditText)findViewById(R.id.sh_yuan)).getText().toString();
			//判断输入不让为空
			if(!Str1.equals("")&&!Str2.equals("")&&!Str3.equals("")&&!Str4.equals("")){
				Intent intent = new Intent(Demo1Activity.this,Demo2Activity.class);
			//创建并实例化	Bundle对象
			Bundle bundle  = new Bundle();
			bundle.putCharSequence("name", Str1);
			bundle.putCharSequence("age", Str2);
			bundle.putCharSequence("sex", Str3);
			bundle.putCharSequence("yuan", Str4);			
			intent.putExtras(bundle);			
			startActivity(intent);

			}else{				
				Toast.makeText(Demo1Activity.this, "输入信息有误,请重新输入", Toast.LENGTH_SHORT).show();				
			}	
			}
		});	
	}
}

5.3,demo2Activity.xml

一个TextView用来展示数据,Button用来返回
    <TextView
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示学生信息"
        android:textSize="30dp"        
         />
    <TextView
        android:id="@+id/show_demo1"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:visibility="invisible" 
        />
    <Button 
        android:id="@+id/btn_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回"
        />

5.3,demo2Activity.java

原理,用getIntent(获取Intent对象,用getExtras()获取Bundle对象,用getString(“key”)获取值,把获取的值用setText输出
finish();----结束活动,返回上一层

public class Demo2Activity extends Activity {
	
	TextView show_demo1;
	Button btnok;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_demo2);
		
		Intent intent = getIntent();
		Bundle bundle =intent.getExtras();	
		show_demo1 = (TextView) findViewById(R.id.show_demo1); 	
		String str ="姓名:"+bundle.getString("name")+"\n"+"年龄:"+bundle.getString("age")+"\n"+
		"性别:"+bundle.getString("sex")+"\n"+"学院:"+bundle.getString("yuan");
		show_demo1.setText(str);
		btnok = (Button) findViewById(R.id.btn_ok);
		show_demo1.setVisibility(1);
		btnok.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub		
				//返回
				finish();	
			}
		});	
	}
}

效果:::
在这里插入图片描述

6,直接建立数据源,传给另一个活动

demo1.java

数据源
String[] str = new String[]{"张三","18","男"};


放进Bundle里
				Bundle bundle = new Bundle();//创建并实例化一个bundle对象
				bundle.putCharSequence("name", str[0]);
				bundle.putCharSequence("age", str[1]);
				bundle.putCharSequence("sex", str[2]);
				//注意::putExtras::复数
				intent.putExtras(bundle);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值