1.使用数据传递实现用户注册,数据回传实现金额充值.
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context=".MainActivity" > 8 9 <LinearLayout 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_marginLeft="100dp" 13 android:layout_marginTop="50dp"> 14 <TextView 15 android:id="@+id/tv_1" 16 android:layout_width="50dp" 17 android:layout_height="wrap_content" 18 android:text="用户名:"/> 19 <EditText 20 android:id="@+id/et_1" 21 android:layout_width="120dp" 22 android:layout_height="wrap_content" 23 android:hint="请输入用户名"/> 24 </LinearLayout> 25 26 <LinearLayout 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:layout_marginLeft="100dp" 30 android:layout_marginTop="10dp"> 31 <TextView 32 android:id="@+id/tv_2" 33 android:layout_width="50dp" 34 android:layout_height="wrap_content" 35 android:text="密码:"/> 36 <EditText 37 android:id="@+id/et_2" 38 android:layout_width="120dp" 39 android:layout_height="wrap_content" 40 android:hint="请输入密码"/> 41 </LinearLayout> 42 43 <TextView 44 android:id="@+id/t3" 45 android:layout_width="wrap_content" 46 android:layout_height="wrap_content" 47 android:layout_marginTop="50dp" 48 android:layout_marginLeft="50dp" 49 android:text="兴趣爱好:"/> 50 51 <CheckBox 52 android:id="@+id/cb_1" 53 android:layout_width="wrap_content" 54 android:layout_height="wrap_content" 55 android:layout_marginTop="10dp" 56 android:layout_marginLeft="50dp" 57 android:text="编程"/> 58 59 <CheckBox 60 android:id="@+id/cb_2" 61 android:layout_width="wrap_content" 62 android:layout_height="wrap_content" 63 android:layout_marginLeft="50dp" 64 android:text="下棋"/> 65 66 <CheckBox 67 android:id="@+id/cb_3" 68 android:layout_width="wrap_content" 69 android:layout_height="wrap_content" 70 android:layout_marginLeft="50dp" 71 android:text="唱歌"/> 72 73 <Button 74 android:id="@+id/btn_1" 75 android:layout_width="wrap_content" 76 android:layout_height="wrap_content" 77 android:layout_marginTop="50dp" 78 android:layout_marginLeft="150dp" 79 android:text="注册" /> 80 81 </LinearLayout>
1 package com.example.week9; 2 3 import android.os.Bundle; 4 import androidx.appcompat.app.AppCompatActivity; 5 import android.content.Intent; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.CheckBox; 10 import android.widget.CompoundButton; 11 import android.widget.Toast; 12 13 public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{ 14 15 String s1="",s2="",s3=""; 16 private EditText et_1; 17 private Button btn_1; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 et_1=findViewById(R.id.et_1); 25 btn_1=findViewById(R.id.btn_1); 26 27 CheckBox cb1=findViewById(R.id.cb_1); 28 CheckBox cb2=findViewById(R.id.cb_2); 29 CheckBox cb3=findViewById(R.id.cb_3); 30 cb1.setOnCheckedChangeListener(this); 31 cb2.setOnCheckedChangeListener(this); 32 cb3.setOnCheckedChangeListener(this); 33 34 btn_1.setOnClickListener(new View.OnClickListener() { 35 @Override 36 public void onClick(View view) { 37 passDate(); 38 } 39 }); 40 } 41 42 public void passDate(){ 43 // 用intent封装数据并传递 44 Intent intent=new Intent(this,SecondActivity.class); 45 46 intent.putExtra("name",et_1.getText().toString().trim()); 47 48 String text=s1+s2+s3; 49 intent.putExtra("text",text); 50 startActivity(intent); 51 } 52 53 @Override 54 public void onCheckedChanged(CompoundButton cb, boolean isChecked) { 55 // TODO Auto-generated method stub 56 switch (cb.getId()) { 57 case R.id.cb_1: 58 if (isChecked) 59 s1 += "编程"; 60 else s1 = ""; 61 break; 62 case R.id.cb_2: 63 if (isChecked) 64 s2 += "下棋"; 65 else s2 = ""; 66 break; 67 case R.id.cb_3: 68 if (isChecked) 69 s3 += "唱歌"; 70 else s3 = ""; 71 break; 72 default: 73 break; 74 } 75 } 76 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".SecondActivity" > 7 8 <TextView 9 android:id="@+id/tv_3" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_marginTop="80dp" 13 android:layout_marginLeft="150dp" 14 android:text="." /> 15 16 <TextView 17 android:id="@+id/tv4" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_marginTop="110dp" 21 android:layout_marginLeft="150dp" 22 android:text="" /> 23 24 <Button 25 android:id="@+id/btn_2" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:layout_marginTop="150dp" 29 android:layout_marginLeft="150dp" 30 android:onClick="click" 31 android:text="我要充值" /> 32 33 <TextView 34 android:id="@+id/tv_4" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:layout_marginTop="230dp" 38 android:layout_marginLeft="150dp" 39 android:text="" /> 40 41 </RelativeLayout>
1 package com.example.week9; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.TextView; 8 9 public class SecondActivity extends AppCompatActivity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_second); 15 16 Intent intent=getIntent(); 17 String name= intent.getStringExtra("name"); 18 String text="用户名:"+name; 19 ((TextView)(findViewById(R.id.tv_3))).setText(text); 20 21 String xq=intent.getStringExtra("text"); 22 String text1="兴趣爱好:"+xq; 23 ((TextView)(findViewById(R.id.tv4))).setText(text1); 24 } 25 26 public void click(View view) { 27 // 设置跳转到的Activity 28 Intent intent=new Intent(this,ThirdActivity.class); 29 startActivityForResult(intent,1); 30 } 31 @Override 32 protected void onActivityResult(int requestCode,int resultCode,Intent intent){ 33 super.onActivityResult(requestCode, resultCode, intent); 34 if(requestCode==1&resultCode==2){ 35 int rmb=intent.getIntExtra("rmb",0); 36 TextView textView=(findViewById(R.id.tv_4)); 37 String text="充值金额为:"+rmb; 38 textView.setText(text); 39 } 40 } 41 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".ThirdActivity"> 7 8 <LinearLayout 9 android:id="@+id/llt_1" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content"> 12 <TextView 13 android:id="@+id/tv_5" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_marginTop="100dp" 17 android:layout_marginLeft="100dp" 18 android:text="充值:"/> 19 <EditText 20 android:id="@+id/et_3" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:layout_marginTop="100dp" 24 android:hint="请输入充值金额"/> 25 </LinearLayout> 26 27 <Button 28 android:id="@+id/btn_3" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:layout_marginLeft="150dp" 32 android:layout_below="@id/llt_1" 33 android:onClick="click2" 34 android:text="充值"/> 35 36 </RelativeLayout>
1 package com.example.week9; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.TextView; 8 9 public class ThirdActivity extends AppCompatActivity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_third); 15 } 16 17 public void click2(View view){ 18 Intent intent=new Intent(); 19 TextView textView=(findViewById(R.id.et_3)); 20 int rmb=Integer.parseInt(textView.getText().toString()); 21 22 intent.putExtra("rmb",rmb); 23 setResult(2,intent); 24 finish(); 25 } 26 }