android 实现页面跳转及数据的传递和返回

1.实现效果:

原始界面:     ----传输数据----------> 填写数据后,点击计算后界面-----返回数据----->点击返回按钮后,回到上一个页面,依旧能够保留之前保持的数据

                                 

2.实现代码:

a.两个布局文件:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<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"
android:orientation="vertical"
tools:context="com.example.activity_return.MainActivity">
<TextView
android:textSize="30sp"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
<LinearLayout
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:text="@string/sex"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:checked="true"
android:layout_marginLeft="10dp"
android:id="@+id/rb1"
android:text="@string/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:layout_marginLeft="30dp"
android:id="@+id/rb2"
android:text="@string/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="@string/height"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et"
android:layout_marginLeft="10dp"
android:background="@drawable/bg_edittext"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:layout_marginLeft="10dp"
android:text="cm"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:layout_marginTop="50dp"
android:layout_marginLeft="100dp"
android:id="@+id/bt"
android:text="@string/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

new_activity.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/text"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="返回"
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

b.两个activity.java文件:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private EditText et;
private RadioButton rb1;
private RadioButton rb2;
private Button bt;
private Double height;
private String sex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化控件
initData();
//实现跳转
jump();
}
private void initData(){
et=(EditText)findViewById(R.id.et);
bt=(Button)findViewById(R.id.bt);
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);

}
private void jump(){
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str=et.getText().toString();
//一定要注意这一步的判断,因为用户可能没有填写身高就提交了,那么这种情况下会导致程序奔溃
if(!str.equals("")) height=Double.parseDouble(et.getText().toString());
else{
et.setHint("请输入身高");
return;
}
//那么对于这种情况,我们可以在布局文件中先设置某个按钮默认的checked为true,然后根据用户来更改
if(rb1.isChecked()){
sex="M";
}else{
sex="F";
}
Intent intent=new Intent();
intent.setClass(MainActivity.this,New_Activity.class);
//利用bundle来存取数据
Bundle bundle=new Bundle();
bundle.putDouble("height",height);
bundle.putString("sex",sex);
//再把bundle中的数据传给intent,以传输过去
intent.putExtras(bundle);
startActivityForResult(intent,0);
}
});
}
//这里是设置获取从第二页面中返回的数据,如果我们没有设置这个的话,我们返回该页面,那么数据都会清空
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){//如果是返回的标识
//获取数据
Bundle bundle=data.getExtras();
sex=bundle.getString("sex");
height=bundle.getDouble("height");
//保留之前的数据
if(sex.equals("M")){
rb1.setChecked(true);
}else{
rb2.setChecked(true);
}
String str=height.toString();
et.setText(str);
}
}
}
new_activity.java:
public class New_Activity extends Activity {
private TextView textView;
private String sex;
private String sexText;
private Double height;
private String weight;
private Button button;
private Intent intent;
private Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
initData();
//设置返回上一个页面的数据
setBackData();
}
private void initData(){
textView=(TextView)findViewById(R.id.text);
button=(Button)findViewById(R.id.button1);
//获取上个页面传输过来的数据放在intent中
intent=this.getIntent();
bundle=intent.getExtras();
sex=bundle.getString("sex");
height=bundle.getDouble("height");
if(sex.equals("M")){
sexText="男性";
}else{
sexText="女性";
}
getWeight();
}
private void getWeight(){
if(sex.equals("M")){
weight=(height-80)*0.7+"";
}else{
weight=(height-70)*0.6+"";
}
}
private void setBackData(){
textView.setText("你是一位"+sexText+"\n你的身高是"+height+"厘米\n你的标准体重是"+weight+"公斤");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//因为我们在initData中已经将传输过来的数据放在intent中,所以这里我们直接用intent即可
setResult(RESULT_OK,intent);
finish();
}
});
}
}

 

转载于:https://www.cnblogs.com/xy95/p/5860698.html

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值