第八次作业 手机充值数据回传
Aactivity.java
1 package com.example.a1023; 2 import androidx.appcompat.app.AppCompatActivity; 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.EditText; 8 import android.widget.Toast; 9 public class MainActivity extends AppCompatActivity { 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 Button bt = (Button) findViewById(R.id.bt1); 15 bt.setOnClickListener(new View.OnClickListener() { 16 @Override 17 public void onClick(View view) { 18 EditText et1 = (EditText) findViewById(R.id.et1); 19 String et_1 = et1.getText().toString(); 20 Intent intent = new Intent(); 21 intent.setClass(MainActivity.this, czActivity.class); 22 Bundle bundle = new Bundle(); 23 bundle.putString("nb", et_1); 24 intent.putExtras(bundle); 25 startActivityForResult(intent,1); 26 27 } 28 }); 29 } 30 @Override 31 protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 32 super.onActivityResult(requestCode, resultCode, intent); 33 // RESULT_OK,判断另外一个activity已经结束数据输入功能,Standard activity result: 34 // operation succeeded. 默认值是-1 35 if (resultCode == 2) { 36 String je = intent.getStringExtra("money"); 37 if (je.equals("0")) { 38 Toast.makeText(MainActivity.this, "充值失败", Toast.LENGTH_SHORT).show(); 39 } else { 40 41 Toast.makeText(MainActivity.this, "充值金额" + je, Toast.LENGTH_SHORT).show(); 42 } 43 } 44 } 45 }
A.xml
<?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" tools:context=".MainActivity"> <LinearLayout android:id="@+id/line1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入手机号码:" /> <EditText android:id="@+id/et1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="" /> </LinearLayout> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/line1" android:layout_centerHorizontal="true" android:layout_marginTop="44dp" android:text="充值" /> </RelativeLayout>
B.java
package com.example.a1023; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class czActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cz); Bundle bundle=getIntent().getExtras(); String n=bundle.getString("nb"); TextView tv2=findViewById(R.id.t2); tv2.setText(n); } public void qd(View view) { EditText et2=findViewById(R.id.et2); String et_2= et2.getText().toString(); Intent intent=new Intent(czActivity.this,MainActivity.class); intent.putExtra("money",et_2); setResult(2,intent); finish(); } public void qx(View view) { EditText et2=findViewById(R.id.et2); String et_2= et2.getText().toString(); Intent intent=new Intent(czActivity.this,MainActivity.class); intent.putExtra("money",et_2); setResult(2,intent); finish(); } }
B.xml
<?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" tools:context=".czActivity"> <LinearLayout android:id="@+id/l2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_centerHorizontal="true" android:layout_marginTop="200dp"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你即将为:" /> <TextView android:id="@+id/t2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" /> <TextView android:id="@+id/t3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="充值"> </TextView> </LinearLayout> <LinearLayout android:id="@+id/lin3" android:layout_below="@+id/l2" android:layout_marginTop="30sp" android:layout_centerHorizontal="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="充值金额:"> </TextView> <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="0" android:hint="请输入充值金额"> </EditText> </LinearLayout> <LinearLayout android:layout_below="@+id/lin3" android:layout_marginTop="40sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_centerHorizontal="true"> <Button android:layout_width="wrap_content" android:id="@+id/bt2" android:text="确定充值" android:layout_height="wrap_content" android:onClick="qd"> </Button> <Button android:layout_width="wrap_content" android:layout_marginLeft="80sp" android:id="@+id/bt3" android:text="取消充值" android:layout_height="wrap_content" android:onClick="qx"> </Button> </LinearLayout> </RelativeLayout>