Android从零开始(十四)

知识点:TabHost选项卡之间传值(将选项卡一中EditText通过Bundle传递到选项卡二中)

步骤一、选项卡一拿值存值再设置当前选项卡为二(核心代码如下)
package com.veryedu.tabhost;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Tab1Activity extends Activity {
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MainActivity.index=0;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lab1);
		
		Button btn=(Button)findViewById(R.id.btn);
		btn.setOnClickListener(new  View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				EditText editText=(EditText)findViewById(R.id.edit_text);
	            String msg=editText.getText().toString();
	            MainActivity.bundle.putString("msg", msg);
	            MainActivity.tabHost.setCurrentTab(R.layout.lab2);
				
				
			}
		});
	}
	
	

}


步骤二、选项卡二拿值(核心代码如下)
package com.veryedu.tabhost;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Tab2Activity extends Activity {

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MainActivity.index=1;
		TextView textView=(TextView)findViewById(R.id.text_view);
		textView.setText("从布局一传递过来的值是:"+MainActivity.bundle.getString("msg"));
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lab2);
		
		
	}
	
	

}


源码下载请点击这里:





你可能感兴趣的:(android,UI.TabHost)