安卓第五次作业

package com.example.myapplication;
 
import androidx.appcompat.app.AppCompatActivity;
 
 
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
 
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
 
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,
        CompoundButton.OnCheckedChangeListener, View.OnClickListener
{String a="",b="",c="",sex="";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_1=findViewById(R.id.btn_1);//按钮一
        btn_1.setOnClickListener(this);
        CheckBox cb_1=findViewById(R.id.cb_1);//复选1
        cb_1.setOnCheckedChangeListener(this);
        CheckBox cb_2=findViewById(R.id.cb_2);//复选2
        cb_2.setOnCheckedChangeListener(this);
        CheckBox cb_3=findViewById(R.id.cb_3);//复选3
        cb_3.setOnCheckedChangeListener(this);
        RadioGroup rg_1=findViewById(R.id.rg_1);
        rg_1.setOnCheckedChangeListener(this);//单选
 
 
    }
 
    @Override
    public void onClick(View view) {
        Intent intent=new Intent(this,SecondActivity.class);
        String you=((EditText)findViewById(R.id.et_1)).getText().toString();
        String mim=((EditText)findViewById(R.id.et_2)).getText().toString();
        String hob=a+b+c;
        intent.putExtra("yhm",you);
        intent.putExtra("mi",mim);
        intent.putExtra("hob",hob);
        intent.putExtra("sex",sex);
        startActivity(intent);
 
    }
 
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean q) {
        switch(compoundButton.getId()){
            case R.id.cb_1:
                if(q)
                    a="足球";
                else
                    a="";
                break;
            case R.id.cb_2:
                if(q)
                    b="篮球";
                else
                    b="";
                break;
            case R.id.cb_3:
                if(q)
                    c="乒乓球";
                else
                    c="";
                break;
        }
 
    }//复选
 
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.rd_1:
                sex="男";
                break;
            case  R.id.rd_2:
                sex="女";
 
 
        }
 
    }//单选
}

package com.example.myapplication;
 
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);
        Button btn_2=findViewById(R.id.btn_2);//按钮2
        btn_2.setOnClickListener(this);
        Intent intent=getIntent();
        String mz=intent.getStringExtra("yhm");
        String mm=intent.getStringExtra("mi");
        String hob=intent.getStringExtra("hob");
        String sex=intent.getStringExtra("sex");
        TextView tv1=findViewById(R.id.tv_1);
        tv1.setText("用户名为:"+mz+",密码为"+mm);
         TextView tv2=findViewById(R.id.tv_2);
         tv2.setText("爱好是为:"+hob+",性别为"+sex);
 
 
 
 
    }
 
    @Override
    public void onClick(View view) {
        Intent intent=new Intent(this,ThirdActivity.class);
       startActivityForResult(intent,1);
 
 
    }
 
    @Override
    protected void onActivityResult(int r, int t, @Nullable Intent intent) {
        if(r==1&&t==1){
 
            String cz=intent.getStringExtra("cz");
            //显示在tv3上
            TextView tv3=findViewById(R.id.tv_3);
            tv3.setText("你的充值金额是:"+cz);}
 
    }
}

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.EditText;
 
public class ThirdActivity extends AppCompatActivity implements View.OnClickListener {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        Button btn_3=findViewById(R.id.btn_3);
        btn_3.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View view) {
        EditText co=findViewById(R.id.et_cz);
        String cz=co.getText().toString();
        Intent intent=new Intent();
        intent.putExtra("cz", cz);
        setResult(1, intent);
        finish();
    }
}

posted @ 2021-11-02 11:55  MaLD  阅读(35)  评论(0编辑  收藏  举报