标签:support val intent play divide ttext 初始 tor 多个参数
首先在主界面设置按钮点击事件,点击按钮后就跳转到计算活动中,此时用到了Bundle用来传递多个参数(与intent对象配合使用,是由一种活动之间传参的方法)
btnHight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,CHActivity.class); Bundle bundle = new Bundle(); bundle.putString("Father" ,"180"); bundle.putString("Mom","165"); intent.putExtras(bundle); //startActivity(intent); startActivityForResult(intent,1); } });
在另一个活动中接收数据
//获取前一个activity的初始数据 Intent intent = getIntent(); Bundle bundle = intent.getExtras(); etFather.setText(bundle.getString("Father")); etMom.setText(bundle.getString("Mom"));
然后通过按钮点击事件,利用身高推测公式计算身高(儿子身高(厘米) =(父亲身高+母亲身高 ×1.08)÷2 女儿身高(厘米) =(父亲身高×0.923+母亲身高)÷2)
//计算身高并返回结果按钮的点击事件 btnResult.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取值,获取父亲身高和母亲身高 int intFather = 0; int intMom = 0; int intBoy = 0; int intGirl = 0; intFather = Integer.parseInt(etFather.getText().toString()); intMom = Integer.parseInt(etMom.getText().toString()); //计算 intBoy = (int)((intFather + intMom * 1.08) / 2); intGirl = (int)((intMom + intFather*0.923) / 2); //回显 tvResult.setText("男孩:" + String.valueOf(intBoy) + "女孩:" + String.valueOf(intGirl)); //Intent intent1 = new Intent(); //intent1.putExtra("Boy" , intBoy); //intent1.putExtra("Girl",intGirl); //setResult(10,intent1); //finish(); } });
计算结果回传到前一个活动中
Intent intent1 = new Intent(); intent1.putExtra("Boy" , intBoy); intent1.putExtra("Girl",intGirl); setResult(10,intent1); finish();
进行到这一步之后,由于用到了数据的回传,所以之前的活动跳转语句就不适用了,为了获取回传的数据,需要将startActivity(intent)改为tartActivityForResult(intent,1)
//startActivity(intent); startActivityForResult(intent,1);
获取结果,并在界面显示:
//获取结果事件 @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==1) { if(resultCode==10){ tvMsg.setText("男孩:" + String.valueOf(data.getIntExtra("Boy",0)) + " 女孩:" + String.valueOf(data.getIntExtra("Girl",0)) ); } } }
完整代码:
package com.example.hight; import android.content.Intent; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { //初始化变量 private TextView tvMsg = null; private Button btnHight = null; //事件 //创建事件 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnHight = (Button)findViewById(R.id.btnMainHeight); tvMsg = (TextView)findViewById(R.id.tvMainResult); btnHight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,CHActivity.class); Bundle bundle = new Bundle(); bundle.putString("Father" ,"180"); bundle.putString("Mom","165"); intent.putExtras(bundle); //startActivity(intent); startActivityForResult(intent,1); } }); } //获取结果事件 @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==1) { if(resultCode==10){ tvMsg.setText("男孩:" + String.valueOf(data.getIntExtra("Boy",0)) + " 女孩:" + String.valueOf(data.getIntExtra("Girl",0)) ); } } } }
package com.example.hight; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; @SuppressLint("Registered") public class CHActivity extends AppCompatActivity { //定义变量 private EditText etFather = null; private EditText etMom = null; private Button btnResult = null; private TextView tvResult = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ch); etFather = (EditText)findViewById(R.id.etCHFather); etMom = (EditText)findViewById(R.id.etChMom); btnResult = (Button)findViewById(R.id.btnCHCount); tvResult = (TextView)findViewById(R.id.tvChResult); //获取前一个activity的初始数据 Intent intent = getIntent(); Bundle bundle = intent.getExtras(); etFather.setText(bundle.getString("Father")); etMom.setText(bundle.getString("Mom")); //计算身高并返回结果按钮的点击事件 btnResult.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取值,获取父亲身高和母亲身高 int intFather = 0; int intMom = 0; int intBoy = 0; int intGirl = 0; intFather = Integer.parseInt(etFather.getText().toString()); intMom = Integer.parseInt(etMom.getText().toString()); //计算 intBoy = (int)((intFather + intMom * 1.08) / 2); intGirl = (int)((intMom + intFather*0.923) / 2); //回显 tvResult.setText("男孩:" + String.valueOf(intBoy) + "女孩:" + String.valueOf(intGirl)); Intent intent1 = new Intent(); intent1.putExtra("Boy" , intBoy); intent1.putExtra("Girl",intGirl); setResult(10,intent1); finish(); } }); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/tvMainResult" android:layout_width="match_parent" android:layout_height="43dp" android:layout_alignParentBottom="true" android:layout_marginLeft="31dp" android:layout_marginBottom="369dp" android:text="结果" android:textColor="@color/colorAccent" /> <Button android:id="@+id/btnMainHeight" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginStart="31dp" android:layout_marginLeft="31dp" android:layout_marginTop="318dp" android:text="计算" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="79dp" tools:layout_editor_absoluteY="313dp" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="vertical"> <TextView android:id="@+id/tvCHFather" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignStart="@+id/etCHFather" android:layout_alignParentTop="true" android:layout_marginStart="0dp" android:layout_marginTop="35dp" android:text="父亲身高(cm)" tools:layout_editor_absoluteX="48dp" tools:layout_editor_absoluteY="48dp" /> <EditText android:id="@+id/etCHFather" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginStart="16dp" android:layout_marginLeft="16dp" android:layout_marginTop="70dp" android:ems="10" android:inputType="textPersonName" android:text="0" tools:layout_editor_absoluteX="38dp" tools:layout_editor_absoluteY="107dp" /> <TextView android:id="@+id/tvCHMom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginStart="19dp" android:layout_marginLeft="19dp" android:layout_marginTop="129dp" android:text="母亲身高(cm)" tools:layout_editor_absoluteX="38dp" tools:layout_editor_absoluteY="194dp" /> <EditText android:id="@+id/etChMom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignStart="@+id/btnCHCount" android:layout_alignParentTop="true" android:layout_marginStart="-7dp" android:layout_marginTop="168dp" android:ems="10" android:inputType="textPersonName" android:text="0" tools:layout_editor_absoluteX="38dp" tools:layout_editor_absoluteY="248dp" /> <Button android:id="@+id/btnCHCount" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" android:layout_marginStart="28dp" android:layout_marginLeft="28dp" android:layout_marginBottom="137dp" android:text="计算身高并返回结果" tools:layout_editor_absoluteX="53dp" tools:layout_editor_absoluteY="369dp" /> <TextView android:id="@+id/tvChResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignStart="@+id/btnCHCount" android:layout_centerVertical="true" android:text="计算结果" /> </RelativeLayout>
标签:support val intent play divide ttext 初始 tor 多个参数
原文地址:https://www.cnblogs.com/1gaoyu/p/12601977.html