Android 界面跳转及数据交换

日期:2019-02-06 编辑:AG88

Android 界面跳转及数据交换

 

 

本文演示:Android 界面跳转及数据交换,通过一个小Demo展示全部过程。

效果如下所示:

1)MainActivity.java

 

package doogle.xian.bundletest;

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;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				EditText name = (EditText)findViewById(R.id.name);
				EditText age = (EditText)findViewById(R.id.age);
				CData send = new CData(name.getText().toString(),Integer.parseInt(age.getText().toString())); 
				//创建一个Bundle对象
				Bundle data = new Bundle();
				data.putSerializable(mydata, send);
				//创建一个Intent对象
				Intent intent = new Intent(MainActivity.this,ResultActivity.class);
				intent.putExtras(data);
				//启动Intent对应的Activity
				startActivity(intent);
			}
		});
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

2)activity_main.xml

 

 



    
    
    

3)ResultActivity.java

 

 

package doogle.xian.bundletest;

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

public class ResultActivity extends Activity {
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        TextView tv1 = (TextView)findViewById(R.id.tv1);
        TextView tv2 = (TextView)findViewById(R.id.tv2);
        //获得启动该Result的Intent
        Intent intent = getIntent();
        //直接通过Intent取出它所携带的Bundle数据包中的数据
        CData accept = (CData)intent.getSerializableExtra(mydata);
        tv1.setText(姓名+accept.getName());
        tv2.setText(年龄+accept.getAge());
    }
}
4)result.xml

 

 



    
    
    


5)CData.java

 

 

package doogle.xian.bundletest;

import java.io.Serializable;

public class CData implements Serializable{
	private static final long serialVersionUID = 1L;
	String name;
	int age;
	public CData(String n, int a) {
		name = n;
		age = a;
	}
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return age;
	}
}
6)AndroidMainfest.xml

 

 



    

    
        
            
                

                
            
        
        
        
        
    



原文地址:http://blog.csdn.net/qingdujun/article/details/40015757

 

http://www.bkjia.com/Androidjc/893481.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/893481.htmlTechArticleAndroid 界面跳转及数据交换 本文演示:Android 界面跳转及数据交换,通过一个小Demo展示全部过程。 效果如下所示: 1)MainActivity.java packag...

本文源自: AG环亚官网