第八次作业
<?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/tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:textSize="25dp" android:layout_marginLeft="50dp" android:text="用户名:"/> <EditText android:id="@+id/et_1" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_1" android:layout_marginTop="100dp" android:hint="请输入用户名"/> <TextView android:id="@+id/tv_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_1" android:layout_marginTop="20dp" android:textSize="25dp" android:layout_marginLeft="50dp" android:text="密 码:"/> <EditText android:id="@+id/et_2" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_2" android:layout_below="@id/et_1" android:hint="请输入密码"/> <TextView android:id="@+id/tv_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="兴趣爱好:" android:textSize="25dp" android:layout_below="@id/tv_2" android:layout_marginTop="30dp"/> <CheckBox android:id="@+id/cb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="编程" android:textSize="20dp" android:layout_below="@id/tv_3"/> <CheckBox android:id="@+id/cb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下棋" android:textSize="20dp" android:layout_below="@id/cb_1"/> <CheckBox android:id="@+id/cb_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="唱歌" android:textSize="20dp" android:layout_below="@id/cb_2"/> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:layout_below="@id/cb_3" android:layout_centerHorizontal="true" android:onClick="btn1"/> </RelativeLayout>
<?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=".Main2Activity"> <Button android:id="@+id/bt_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我要充值" android:layout_centerInParent="true" android:onClick="btn2"/> <TextView android:id="@+id/tv_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:layout_centerHorizontal="true"/> <TextView android:id="@+id/tv_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/tv_4" android:layout_marginTop="20dp"/> <TextView android:id="@+id/tv_6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/bt_2" android:layout_marginTop="20dp"/> </RelativeLayout>
<?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=".Main3Activity"> <EditText android:id="@+id/et_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入充值金额"/> <Button android:id="@+id/bt_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="完成" android:layout_centerInParent="true" android:onClick="btn3"/> </RelativeLayout>
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.bt_1); 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 btn1(View view){ Intent intent=new Intent(MainActivity.this,Main2Activity.class); EditText editText=(EditText)findViewById(R.id.et_1); 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.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; } } }
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 Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); TextView textview=(TextView)findViewById(R.id.tv_4); TextView textView1=findViewById(R.id.tv_5); 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(Main2Activity.this,Main3Activity.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.tv_6)); 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 Main3Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); } public void btn3(View view){ Intent intent=new Intent(); TextView textView=(TextView)(findViewById(R.id.et_3)); int price=Integer.parseInt(textView.getText().toString()); intent.putExtra("price", price); setResult(222, intent); finish(); } }