Android第9周练习
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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:id="@+id/ll_id" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:layout_marginLeft="20dp" 14 android:layout_marginTop="100dp" 15 android:layout_marginRight="20dp"> 16 17 <TextView 18 android:id="@+id/tv_id1" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="用户名:" 22 23 android:textSize="30sp" /> 24 25 <EditText 26 android:id="@+id/et_id" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:hint="请输入用户名" 30 android:minLines="1" /> 31 32 </LinearLayout> 33 34 <LinearLayout 35 android:id="@+id/ll_pass" 36 android:layout_width="match_parent" 37 android:layout_height="wrap_content" 38 android:layout_below="@id/ll_id" 39 android:layout_marginLeft="20dp" 40 android:layout_marginTop="20dp" 41 android:layout_marginRight="20dp"> 42 43 <TextView 44 android:id="@+id/tv_pass" 45 android:layout_width="wrap_content" 46 android:layout_height="wrap_content" 47 android:layout_marginLeft="30dp" 48 android:text="密码:" 49 android:textSize="30sp" /> 50 51 <EditText 52 android:id="@+id/et_pass" 53 android:layout_width="match_parent" 54 android:layout_height="wrap_content" 55 android:hint="请输入密码 " 56 android:password="true" 57 android:minLines="1" /> 58 59 </LinearLayout> 60 61 <TextView 62 android:id="@+id/tv_xh" 63 android:layout_width="wrap_content" 64 android:layout_height="wrap_content" 65 android:layout_marginLeft="20dp" 66 android:layout_marginTop="30dp" 67 android:text="兴趣爱好:" /> 68 69 <CheckBox 70 android:id="@+id/cb_pro" 71 android:layout_width="wrap_content" 72 android:layout_height="wrap_content" 73 android:layout_marginLeft="30dp" 74 android:layout_marginTop="10dp" 75 android:text="编程" /> 76 77 <CheckBox 78 android:id="@+id/cb_chess" 79 android:layout_width="wrap_content" 80 android:layout_height="wrap_content" 81 android:layout_marginLeft="30dp" 82 android:layout_marginTop="10dp" 83 android:text="下棋" /> 84 85 <CheckBox 86 android:id="@+id/cb_sing" 87 android:layout_width="wrap_content" 88 android:layout_height="wrap_content" 89 android:layout_marginLeft="30dp" 90 android:layout_marginTop="10dp" 91 android:text="唱歌" /> 92 93 <Button 94 android:id="@+id/btn_login" 95 android:layout_width="wrap_content" 96 android:layout_height="wrap_content" 97 android:layout_gravity="center" 98 android:layout_marginTop="100dp" 99 android:text="注册" /> 100 101 </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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=".SecondActivity"> <LinearLayout android:id="@+id/ll_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" android:orientation="horizontal"> <TextView android:id="@+id/yhm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:textSize="30dp" /> <TextView android:id="@+id/tv_id2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text=""/> </LinearLayout> <LinearLayout android:id="@+id/ll_xc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="兴趣爱好:" android:textSize="20dp" /> <TextView android:id="@+id/tv_xc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text=""/> </LinearLayout> <Button android:id="@+id/btn_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" android:text="我要充值" /> <TextView android:id="@+id/tv_money" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="30dp" android:text="充值金额显示" /> </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:layout_weight="5" android:orientation="horizontal" tools:context=".ThirdActivity"> <EditText android:id="@+id/et_money" android:layout_width="0dp" android:layout_height="50dp" android:layout_margin="20dp" android:layout_weight="4" android:minLines="1" android:text="" /> <Button android:id="@+id/btn_money" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginRight="20dp" android:layout_weight="1" android:text="充值" /> </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.textapplication; 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.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener { String cb1 = "",cb2 = "",cb3 = ""; TextView id; String idx; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CheckBox pro = findViewById(R.id.cb_pro); CheckBox chess = findViewById(R.id.cb_chess); CheckBox sing = findViewById(R.id.cb_sing); pro.setOnCheckedChangeListener(MainActivity.this); chess.setOnCheckedChangeListener(MainActivity.this); sing.setOnCheckedChangeListener(MainActivity.this); findViewById(R.id.btn_login).setOnClickListener(MainActivity.this); } @Override public void onClick(View view) { id = findViewById(R.id.et_id); idx = id.getText().toString(); String xc=cb1+cb2+cb3; Toast.makeText(MainActivity.this,"恭喜您,注册成功",Toast.LENGTH_LONG).show(); Intent intent = new Intent(MainActivity.this,SecondActivity.class); intent.putExtra("id",idx); intent.putExtra("xc",xc); startActivity(intent); } @Override public void onCheckedChanged(CompoundButton cb, boolean isChecked) { // TODO Auto-generated method stub switch (cb.getId()) { case R.id.cb_pro: if(isChecked) cb1+="编程;"; else cb1=""; break; case R.id.cb_chess: if(isChecked) cb2+="下棋;"; else cb2=""; break; case R.id.cb_sing: if(isChecked) cb3+="唱歌;"; else cb3=""; break; default: break; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.textapplication; import androidx.annotation.Nullable; 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 { TextView tv_id,tv_money,tv_xc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); tv_id = findViewById(R.id.tv_id2); tv_money = findViewById(R.id.tv_money); tv_xc = findViewById(R.id.tv_xc); Intent intent=getIntent(); String id = intent.getStringExtra("id"); String xc = intent.getStringExtra("xc"); tv_id.setText(id); tv_xc.setText(xc); findViewById(R.id.btn_top).setOnClickListener(SecondActivity.this); } @Override public void onClick(View view) { Intent intent2 = new Intent(SecondActivity.this, ThirdActivity.class); startActivityForResult(intent2,1); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1&&resultCode == 2) { String requestCodeData = data.getStringExtra("money"); tv_money.setText("充值金额为" + requestCodeData + "元"); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.textapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class ThirdActivity extends AppCompatActivity implements View.OnClickListener { String money; TextView sum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); findViewById(R.id.btn_money).setOnClickListener(ThirdActivity.this); } @Override public void onClick(View view) { sum = findViewById(R.id.et_money); money = sum.getText().toString(); Toast.makeText(ThirdActivity.this,"充值成功",Toast.LENGTH_LONG).show(); Intent intent = new Intent(); intent.putExtra("money",money); setResult(2,intent); finish(); } }