Android第五次作业
<?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"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:layout_marginTop="100dp" android:layout_marginLeft="20dp"/> <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名:" android:layout_toRightOf="@+id/tv1" android:layout_alignTop="@id/tv1" android:layout_marginRight="50dp"/> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" android:layout_below="@+id/tv1" android:layout_marginTop="50dp" android:layout_alignRight="@+id/tv1"/> <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码:" android:layout_toRightOf="@+id/tv2" android:layout_below="@+id/tv1" android:layout_alignTop="@id/tv2" android:layout_marginRight="50dp"/> <LinearLayout android:id="@+id/ll_1" android:layout_width="200dp" android:layout_height="200dp" android:layout_below="@+id/et2" android:orientation="vertical" android:layout_marginLeft="20dp" android:layout_marginTop="30dp"> <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="兴趣爱好:" /> <CheckBox android:id="@+id/cb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="编程" android:layout_marginTop="30dp"/> <CheckBox android:id="@+id/cb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下棋" /> <CheckBox android:id="@+id/cb_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="唱歌" /> </LinearLayout> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="200dp" android:layout_below="@+id/et2" android:onClick="click" android:text="注册" /> </RelativeLayout>
package com.example.myhomework; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; 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); CheckBox cb1=(CheckBox)findViewById(R.id.cb_1); CheckBox cb2=(CheckBox)findViewById(R.id.cb_2); CheckBox cb3=(CheckBox)findViewById(R.id.cb_3); cb1.setOnCheckedChangeListener(this); cb2.setOnCheckedChangeListener(this); cb3.setOnCheckedChangeListener(this); } public void click(View view){ Intent intent=new Intent(); intent.setClass(MainActivity.this,SecondActivity.class); String name=((EditText)(findViewById(R.id.et1))).getText().toString(); String account=name; String text=s1+" "+s2+" "+s3; intent.putExtra("account",account); 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; } } @Override public void onPointerCaptureChanged(boolean hasCapture) { } }
<?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=".SecondActivity"> <TextView android:id="@+id/tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是界面2" android:textSize="25sp" android:textColor="#000000"/> <TextView android:id="@+id/tv_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="用户名:" /> <TextView android:id="@+id/tv_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_2" android:layout_alignTop="@id/tv_2" android:text="" /> <TextView android:id="@+id/tv_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_2" android:layout_alignLeft="@id/tv_2" android:text="兴趣爱好:" /> <TextView android:id="@+id/tv_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_4" android:layout_alignTop="@id/tv_4" android:text="" /> <Button android:id="@+id/btn_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="150dp" android:text="我要充值"/> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_below="@id/btn_1" android:layout_alignLeft="@id/btn_1"/> </RelativeLayout>
package com.example.myhomework; 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 implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent intent1 = getIntent(); String name = intent1.getStringExtra("account"); ((TextView) (findViewById(R.id.tv_3))).setText(name); Intent intent2 = getIntent(); String text = intent2.getStringExtra("text"); ((TextView) (findViewById(R.id.tv_5))).setText(text); findViewById(R.id.btn_1).setOnClickListener(this); } @Override public void onClick(View view) { Intent intent = new Intent(this,ThirdActivity.class); startActivityForResult(intent,1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==1&&resultCode==2){ String pay=data.getStringExtra("pay"); ((TextView)(findViewById(R.id.tv1))).setText(pay); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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=".ThirdActivity" android:orientation="vertical"> <TextView android:id="@+id/tv_6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="充值金额为:" android:textSize="25sp" android:layout_marginTop="50dp" android:layout_gravity="center"/> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="100" android:layout_gravity="center"/> <Button android:id="@+id/bt_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="500" android:layout_gravity="center"/> <Button android:id="@+id/bt_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1000" android:layout_gravity="center"/> </LinearLayout>
package com.example.myhomework; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class ThirdActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); findViewById(R.id.bt_1).setOnClickListener(this); findViewById(R.id.bt_2).setOnClickListener(this); findViewById(R.id.bt_3).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.bt_1: Intent intent1 = new Intent(this, SecondActivity.class); intent1.putExtra("pay", "充值金额为:100元"); setResult(2, intent1); finish(); break; case R.id.bt_2: Intent intent2 = new Intent(this, SecondActivity.class); intent2.putExtra("pay", "充值金额为:500元"); setResult(2, intent2); finish(); break; case R.id.bt_3: Intent intent3 = new Intent(this, SecondActivity.class); intent3.putExtra("pay", "充值金额为:1000元"); setResult(2, intent3); finish(); break; } } @Override public void onPointerCaptureChanged(boolean hasCapture) { } }