安卓第10周
<?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=".MainActivity" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:"/> <EditText android:id="@+id/et_1" android:layout_width="200dp" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:"/> <EditText android:id="@+id/et_2" android:layout_width="200dp" android:layout_height="wrap_content" /> </LinearLayout> <CheckBox android:id="@+id/cb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="足球"/> <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="乒乓球"/> <RadioGroup android:id="@+id/rg_1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/rb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男"/> <RadioButton android:id="@+id/rb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女"/> </RadioGroup> <Button android:id="@+id/btn_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册"/> </LinearLayout> <?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=".SecondActivity" android:orientation="vertical"> <TextView android:id="@+id/tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="111"/> <TextView android:id="@+id/tv_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="222"/> <Button android:id="@+id/btn_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="充值"/> <TextView android:id="@+id/tv_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="充值金额为"/> </LinearLayout> <?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=".ThirdActivity"> <EditText android:id="@+id/et_cq" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="输入要充值的金额"/> <Button android:id="@+id/btn_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="充值" android:layout_toRightOf="@id/et_cq"/> </RelativeLayout>
package com.example.h10; 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.RadioGroup; public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener, View.OnClickListener { String s1="",s2="",s3="",sex=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn=findViewById(R.id.btn_1);//获取按钮1控件 btn.setOnClickListener(this);//控件与方法绑定 CheckBox cb1=findViewById(R.id.cb_1);//获取复选框1控件 cb1.setOnCheckedChangeListener(this);//控件与方法绑定,会自动调用OnCheckedChanged CheckBox cb2=findViewById(R.id.cb_2);//获取复选框2控件 cb2.setOnCheckedChangeListener(this);//控件与方法绑定,会自动调用OnCheckedChanged CheckBox cb3=findViewById(R.id.cb_3);//获取复选框3控件 cb3.setOnCheckedChangeListener(this);//控件与方法绑定,会自动调用OnCheckedChanged RadioGroup rg=findViewById(R.id.rg_1);//获取单选按钮控件 rg.setOnCheckedChangeListener(this);//控件与方法绑定,会自动调用OnCheckedChanged } @Override //第三种实现按钮点击的方式 public void onClick(View view) { Intent intent=new Intent(this,SecondActivity.class); String username=((EditText)findViewById(R.id.et_1)).getText().toString(); String password=((EditText)findViewById(R.id.et_2)).getText().toString(); String hobby=s1+s2+s3; intent.putExtra("用户名",username); intent.putExtra("密码",password); intent.putExtra("爱好",hobby); intent.putExtra("性别",sex); startActivity(intent); } @Override //参数去代表复选框对象,参数2代表选中状态:是,否 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { switch (buttonView.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; } } @Override //参数1代表单选按钮组,参数2代表选项中的id public void onCheckedChanged(RadioGroup group, int checkedId){ switch (checkedId){ case R.id.rd_1: sex="男"; break; case R.id.rd_2: sex="女"; break; } } } package com.example.h10; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; 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 intent=getIntent(); String username=intent.getStringExtra("用户名"); String password=intent.getStringExtra("密码"); String hobby=intent.getStringExtra("爱好"); String sex=intent.getStringExtra("性别"); TextView tv1=findViewById(R.id.tv_1); tv1.setText("用户名为:"+username+",密码为:"+password); TextView tv2=findViewById(R.id.tv_2); tv2.setText("爱好为:"+hobby+",性别为:"+sex); Button btn2=findViewById(R.id.btn_2); btn2.setOnClickListener(this); } @Override public void onClick(View view) { Intent intent=new Intent(this,ThirdActivity.class); startActivityForResult(intent,1);//有数据回传,需要用这种方式启动 } @Override protected void onActivityResult(int r1, int r2, @Nullable Intent intent) { super.onActivityResult(r1, r2, intent); if(r1==1&&r2==1){//取回回传数据 String jine=intent.getStringExtra("金额"); //显示在tv3上 TextView tv3=findViewById(R.id.tv_3); tv3.setText("您的充值金额是:"+jine); } } } package com.example.h10; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class ThirdActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); Button btn3=findViewById(R.id.btn_3); btn3.setOnClickListener(this); } @Override public void onClick(View view) { EditText et3=findViewById(R.id.et_cq);//获取编辑框 String jine=et3.getText().toString();//充值金额 //充值金额数据回传到界面2 Intent intent=new Intent(); intent.putExtra("金额",jine); setResult(1,intent);//设置结果码,回传到界面2接收 finish(); } }