第十周作业
package com.example.myapplication211018; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{ String s1="",s2="",s3=""; private EditText et_1; private Button btn_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_1=findViewById(R.id.et_1); btn_1=findViewById(R.id.btn_1); CheckBox cb1=findViewById(R.id.cb_1); CheckBox cb2=findViewById(R.id.cb_2); CheckBox cb3=findViewById(R.id.cb_3); cb1.setOnCheckedChangeListener(this); cb2.setOnCheckedChangeListener(this); cb3.setOnCheckedChangeListener(this); btn_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { passDate(); } }); } public void passDate(){ // 用intent封装数据并传递 Intent intent=new Intent(this,SecondActivity.class); intent.putExtra("name",et_1.getText().toString().trim()); String text=s1+s2+s3; intent.putExtra("text",text); startActivity(intent); } @Override public void onCheckedChanged(CompoundButton cb, boolean isChecked) { // TODO Auto-generated method stub switch (cb.getId()) { case R.id.cb_1: if (isChecked) s1 += "编程"; else s1 = ""; break; case R.id.cb_2: if (isChecked) s2 += "下棋"; else s2 = ""; break; case R.id.cb_3: if (isChecked) s3 += "唱歌"; else s3 = ""; break; default: break; } } }
<?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=".MainActivity" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:layout_marginTop="50dp"> <TextView android:id="@+id/tv_1" android:layout_width="50dp" android:layout_height="wrap_content" android:text="用户名:"/> <EditText android:id="@+id/et_1" android:layout_width="120dp" android:layout_height="wrap_content" android:hint="请输入用户名"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:layout_marginTop="10dp"> <TextView android:id="@+id/tv_2" android:layout_width="50dp" android:layout_height="wrap_content" android:text="密码:"/> <EditText android:id="@+id/et_2" android:layout_width="120dp" android:layout_height="wrap_content" android:hint="请输入密码"/> </LinearLayout> <TextView android:id="@+id/t3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:layout_marginLeft="50dp" android:text="兴趣爱好:"/> <CheckBox android:id="@+id/cb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="50dp" android:text="音乐"/> <CheckBox android:id="@+id/cb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:text="舞蹈"/> <CheckBox android:id="@+id/cb_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:text="画画"/> <Button android:id="@+id/btn_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:layout_marginLeft="150dp" android:text="注册" /> </LinearLayout>
package com.example.myapplication211018; 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); Intent intent=getIntent(); String name= intent.getStringExtra("name"); String text="用户名:"+name; ((TextView)(findViewById(R.id.tv_3))).setText(text); String xq=intent.getStringExtra("text"); String text1="兴趣爱好:"+xq; ((TextView)(findViewById(R.id.tv4))).setText(text1); } public void click(View view) { // 设置跳转到的Activity Intent intent=new Intent(this,ChongzhiActivity.class); startActivityForResult(intent,1); } @Override protected void onActivityResult(int requestCode,int resultCode,Intent intent){ super.onActivityResult(requestCode, resultCode, intent); if(requestCode==1&resultCode==2){ int rmb=intent.getIntExtra("rmb",0); TextView textView=(findViewById(R.id.tv_4)); String text="充值金额为:"+rmb; textView.setText(text); } } }
<?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="match_parent" android:layout_height="match_parent" tools:context=".SecondActivity" > <TextView android:id="@+id/tv_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="80dp" android:layout_marginLeft="150dp" android:text="." /> <TextView android:id="@+id/tv4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="110dp" android:layout_marginLeft="150dp" android:text="" /> <Button android:id="@+id/btn_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:layout_marginLeft="150dp" android:onClick="click" android:text="我要充值" /> <TextView android:id="@+id/tv_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="230dp" android:layout_marginLeft="150dp" android:text="" /> </RelativeLayout>
package com.example.myapplication211018; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class ChongzhiActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chongzhi); } public void click2(View view){ Intent intent=new Intent(); TextView textView=(findViewById(R.id.et_3)); int rmb=Integer.parseInt(textView.getText().toString()); intent.putExtra("rmb",rmb); setResult(2,intent); finish(); } }
<?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="match_parent" android:layout_height="match_parent" tools:context=".ThirdActivity"> <LinearLayout android:id="@+id/llt_1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:layout_marginLeft="100dp" android:text="充值:"/> <EditText android:id="@+id/et_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:hint="请输入充值金额"/> </LinearLayout> <Button android:id="@+id/btn_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="150dp" android:layout_below="@id/llt_1" android:onClick="click2" android:text="充值"/> </RelativeLayout>