package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { String s1="",s2="",s3=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button)findViewById(R.id.b1); CheckBox cb1=(CheckBox)findViewById(R.id.cb1); CheckBox cb2=(CheckBox)findViewById(R.id.cb2); CheckBox cb3=(CheckBox)findViewById(R.id.cb3); cb1.setOnCheckedChangeListener(this); cb2.setOnCheckedChangeListener(this); cb3.setOnCheckedChangeListener(this); } public void btn1(View view){ Intent intent=new Intent(MainActivity.this,SecondActivity.class); EditText editText=(EditText)findViewById(R.id.et1); String name=editText.getText().toString(); intent.putExtra("name",name); String text=s1+s2+s3; intent.putExtra("text",text); startActivity(intent); } @Override public void onCheckedChanged(CompoundButton cb, boolean isChecked) { switch (cb.getId()) { case R.id.cb1: if(isChecked) s1+="编程"; else s1=""; break; case R.id.cb2: if(isChecked) s2+=" 下棋"; else s2=""; break; case R.id.cb3: if(isChecked) s3+=" 唱歌"; else s3=""; break; default: break; } } }
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); TextView textview=(TextView)findViewById(R.id.tv4); TextView textView1=findViewById(R.id.tv6); Intent intent=getIntent(); String name1=intent.getStringExtra("name"); String text="用户名:"+name1; textview.setText(text); String text1=intent.getStringExtra("text"); String text2="兴趣爱好:"+text1; textView1.setText(text2); } public void btn2(View view){ Intent intent=new Intent(SecondActivity.this,ThirdActivity.class); startActivityForResult(intent, 111); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, intent); if(requestCode==111&&resultCode==222){ int price1=intent.getIntExtra("price",0); TextView textView1=(TextView)(findViewById(R.id.tv5)); String text2="充值金额:"+price1; textView1.setText(text2); } } }
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class ThirdActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); } public void btn3(View view){ Intent intent=new Intent(); TextView textView=(TextView)(findViewById(R.id.edchong)); int price=Integer.parseInt(textView.getText().toString()); intent.putExtra("price", price); setResult(222, intent); finish(); } }
<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" tools:context=".MainActivity" > <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:layout_marginTop="50dp" android:layout_marginBottom="20dp"/> <TextView android:layout_below="@id/tv1" android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" /> <EditText android:id="@+id/et2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tv2" android:layout_alignBottom="@+id/tv2" android:layout_alignLeft="@+id/et1" android:ems="10" android:hint="请输入密码" /> <EditText android:id="@+id/et1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tv1" android:layout_alignBottom="@+id/tv1" android:layout_marginLeft="14dp" android:layout_toRightOf="@+id/tv1" android:ems="10" android:hint="请输入用户名" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="兴趣爱好:" android:id="@+id/tv3" android:layout_marginTop="30dp" android:layout_below="@id/tv2" /> <CheckBox android:layout_marginTop="20dp" android:id="@+id/cb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="编程" android:layout_below="@id/tv3" /> <CheckBox android:id="@+id/cb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下棋" android:layout_below="@id/cb1" /> <CheckBox android:id="@+id/cb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="唱歌" android:layout_below="@id/cb2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/cb3" android:layout_centerHorizontal="true" android:layout_marginTop="34dp" android:text="注册" android:id="@+id/b1" android:onClick="btn1" /> </RelativeLayout>
<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" tools:context=".SecondActivity" > <Button android:id="@+id/b2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:onClick="btn2" android:text="我要充值" /> <TextView android:id="@+id/tv4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/tv6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="230dp" android:layout_marginLeft="150dp" /> <TextView android:id="@+id/tv5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/b2" android:layout_centerHorizontal="true" android:layout_marginTop="51dp" /> </RelativeLayout>
<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" tools:context=".ThirdActivity" > <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入充值金额" android:id="@+id/edchong" /> <Button android:id="@+id/finish" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edchong" android:layout_centerHorizontal="true" android:layout_marginTop="139dp" android:onClick="btn3" android:text="完成" /> </RelativeLayout>