此文主要还是针对刚开始接触的小白,希望对你们有点帮助吧
第一个Activity
package com.example.jsontext;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
//两个输入文本框,一个按钮
EditText tvName;
EditText tvAge;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//findview
tvName=(EditText) findViewById(R.id.name);
tvAge=(EditText) findViewById(R.id.age);
btn=(Button) findViewById(R.id.button1);
//设置内部类的监听
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//获取输入的值,并toString
String name=tvName.getText().toString();
String age=tvAge.getText().toString();
//使用Bundle<KEY,String>---->KEY:标记 String:值
Bundle bundle=new Bundle();
bundle.putString("NAME", name);
bundle.putString("AGE", age);
//打开Activity
Intent intent =new Intent(MainActivity.this,SecondActivity.class);
//讲封装好的bundle传入intent,就完成了数据的跨Activity传递
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
第一个布局:
<pre name="code" class="html"><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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="请输入名字:" />
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:gravity="center"
android:hint="请输入年龄:" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/age"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:text="Button" />
</RelativeLayout>
</pre><p></p><p>第二个Activity:</p><p><pre name="code" class="html">package com.example.jsontext;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class SecondActivity extends Activity {
Entity entity;
TextView tvMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//findview
tvMessage=(TextView) findViewById(R.id.tv_message);
//获得bundle对象后,把数据getgetExtras出来
//getIntent()-->获取相应的传递的数据
Bundle bundle=getIntent().getExtras();
//然后就是根据标记(KEY)取出值就ok
String name=bundle.getString("NAME");
String age=bundle.getString("AGE");
//最后一步就是显示了
tvMessage.setText("姓名:"+name+",年龄:"+age);
/**
* 这个方法还用很多延伸,希望对大家有帮助
* Avater 2015/12/24
*/
}
}
布局:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" >
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>