疯狂Android讲义学习笔记之差缺补漏003Activity与Fragment

1.使用bundler绑定数据

1主布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
    android:text="姓名"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="密码"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/passward"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/male"
            android:text="男"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RadioButton
            android:id="@+id/female"
            android:text="女"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RadioGroup>
    <Button
        android:id="@+id/mybut"
        android:text="start Activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

2Person类
注意这里实现了序列化接口

public class Person implements Serializable {
    private String name;
    private String password;
    private String gender;

    public Person(String name, String password, String gender) {
        this.name = name;
        this.password = password;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

3主activity

public class MainActivity extends AppCompatActivity {
    private EditText name,passward;
    private RadioButton male,female;
    private Button mybut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name=findViewById(R.id.name);
        passward=findViewById(R.id.passward);

        male=findViewById(R.id.male);
        female=findViewById(R.id.female);

        mybut=findViewById(R.id.mybut);

        mybut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String gender=male.isChecked()? "男":"女";
                Person person=new Person(name.getText().toString(),passward.getText().toString(),gender);
                //Log.d("hj", "onClick: "+name.getText().toString()+"  "+passward.getText().toString()+"  "+gender);
                Bundle bundle=new Bundle();
                bundle.putSerializable("person",person);

                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
}

4被启动activity

public class Main2Activity extends AppCompatActivity {
    private TextView name,passward,gender;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        name=findViewById(R.id.name);
        passward=findViewById(R.id.passward);
        gender=findViewById(R.id.gender);

        Intent intent=getIntent();
        Person person=(Person) intent.getSerializableExtra("person");
        Log.d("hj", "onCreate: ----------------");
        name.setText(person.getName());
        passward.setText(person.getPassword());
        gender.setText(person.getGender());
    }
}
2.StartActivityForResult回传数据

1.主布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/passward"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/backinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/mybut"
        android:text="startActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

2.主activity

public class MainActivity extends AppCompatActivity {
    private EditText name,passward;
    private TextView backinfo;
    private Button mybut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name=findViewById(R.id.name);
        passward=findViewById(R.id.passward);

        backinfo=findViewById(R.id.backinfo);
        mybut=findViewById(R.id.mybut);

        mybut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                intent.putExtra("name",name.getText().toString());
                intent.putExtra("passward",passward.getText().toString());
                startActivityForResult(intent,110);
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 110 && resultCode == 120) {
            backinfo.setText(data.getStringExtra("info"));
        }
    }
}

3、次布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/passward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/goback"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/mybut"
        android:text="back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

次activity

public class Main2Activity extends AppCompatActivity {
    private TextView name,passward;
    private EditText goback;
    private Button mybut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        name=findViewById(R.id.name);
        passward=findViewById(R.id.passward);

        goback=findViewById(R.id.goback);

        Intent intentdis=getIntent();
        name.setText(intentdis.getStringExtra("name"));
        passward.setText(intentdis.getStringExtra("passward"));

        mybut=findViewById(R.id.mybut);
        mybut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentback=new Intent();
                intentback.putExtra("info",goback.getText().toString());
                setResult(120,intentback);
                finish();
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值