Intent+SharedPreferences在活动中传递数据

效果图
这里写图片描述

先在activity_main.xml在布局

<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"
    android:orientation="vertical"
    tools:context="com.example.administrator.formapplication.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        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/username"
            android:layout_width="0dp"
            android:layout_weight="1"//运用权重
            android:layout_height="wrap_content"
            android:hint="请输入你的账号名"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        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/password"
            android:layout_width="0dp"
            android:layout_weight="1"//运用权重
            android:layout_height="wrap_content"
            android:hint="请输入你的密码"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >
        <Button
            android:id="@+id/btn_entry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            />
        <Button
            android:id="@+id/btn_register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"
            />
    </LinearLayout>
</LinearLayout>

然后在MainActivity中初始化按钮和视图
public class MainActivity extends AppCompatActivity {

private EditText userName;
private EditText passWord;
private Button btnRegister;
private Button btnEntry;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        userName = (EditText) findViewById(R.id.username);
        passWord = (EditText) findViewById(R.id.password);
        btnEntry = (Button) findViewById(R.id.btn_entry);
        btnRegister = (Button) findViewById(R.id.btn_register);

      }

}
再然后在onCreate方法中为btnRegister的点击事件注册一个监听器
如下所示:

btnRegister.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(MainActivity.this,InputActivity.class);//new一个Intent对象
                    //显示Intent,第一参数是上下文,第二个参数是想要启动的目标活动
                    startActivityForResult(intent,REQUEST_CODE);
                    /*与之对应的是onActivityResult方法,所以需要用到个需求码REQUEST_CODE
                      这时在 MainActivity中定义private final int REQUEST_CODE = 1001;数字是唯一的就行了110都行
                    这里不用startActivion(intent);是为了传递数据*/
                }
            });

准备工作就做完了,接下来就要新建一个InputActivity活动用来写入数据。
在activity_input.xml进行布局,代码如下:

<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"
    android:orientation="vertical"
    tools:context="com.example.administrator.formapplication.InputActivity">

    <EditText
        android:id="@+id/edt_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入你的账号名"/>

    <EditText
        android:id="@+id/edt_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入你的密码"/>
    <EditText
        android:id="@+id/edt_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入你的年龄"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请选择你的性别: "
            android:layout_gravity="center"/>
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/isBoy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"/>
        </RadioGroup>

    </LinearLayout>

    <Button
        android:id="@+id/btn_confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交"/>

</LinearLayout>

这里需要用到RadioGroup和RadioButton来组建单选按钮。

以下为InputActivity的代码:

public class InputActivity extends AppCompatActivity {

    private EditText edtName;
    private EditText edtPassword;
    private EditText edtAge;
    private RadioButton radioBoy;
    private Button btnConfirm;
    private final int RESULT_CODE = 2001;//定义返回码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);

        initView();
    }

    private void initView() {
        edtName = (EditText) findViewById(R.id.edt_name);
        edtPassword = (EditText) findViewById(R.id.edt_password);
        edtAge = (EditText) findViewById(R.id.edt_age);
        radioBoy = (RadioButton) findViewById(R.id.isBoy);
        btnConfirm = (Button) findViewById(R.id.btn_confirm);

        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = edtName.getText().toString();//获得名字
                String password = edtPassword.getText().toString();//获得密码
                int age = Integer.parseInt(edtAge.getText().toString());//获得年龄
                boolean isBoy = radioBoy.isChecked();//获得性别

                //新建一个intent对象intentb用来传递数据
                Intent intentb = new Intent();
                intentb.putExtra("NAME",name);
                intentb.putExtra("PASSWORD",password);
                intentb.putExtra("AGE",age);
                intentb.putExtra("ISBOY",isBoy);
                //并通过putExtra()方法传递一个值,传值方法有点像键值对,第一个参数是键,第二个参数是值

                setResult(RESULT_CODE,intentb);

                finish();//不用单个活动的退出

            }

        });

    }
}

然后在MainActivity中重写onActivityResult()方法中用SharedPreferences短暂的保存数据,这里我用建立saveToSP()来保持代码的观赏性,在MainActivity中写,

 private void saveToSP(String name, String password, int age, boolean isBoy) {
        //new一个SharedPreferences对象
        SharedPreferences sp = getApplicationContext().getSharedPreferences("PERSON",getApplicationContext().MODE_PRIVATE);
        SharedPreferences.Editor  editor = sp.edit();
        editor.putString("NAME",name);//接收名字
        editor.putString("PASSWORD",password);//接收密码
        editor.putInt("AGE",age);//接收年龄
        editor.putBoolean("ISBOY",isBoy);//接收性别

        editor.commit();

    }

最后读写数据,在MainActivity的onCreate()方法中调用一个readFromSP()函数,在MainActivity中新建readFromSP()。

 private void readFromSP() {
        SharedPreferences sp = getApplicationContext().getSharedPreferences("PERSON",getApplicationContext().MODE_PRIVATE);
        String name = sp.getString("NAME","no name");
        //通过getString()的方法获取在是sp中保存的数据
        userName.setText(name);

    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值