实验五Activity回传数据与内部存储

1.Activity序列化数据传递

package zhku.edu.exp5_1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener {
    Button btn_serializabale;
    EditText et_phone,et_name,et_major,et_age;
    int age;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_serializabale=(Button)findViewById(R.id.zerializable);
        btn_serializabale.setOnClickListener(this);

        et_phone=(EditText)findViewById(R.id.et_phone);
        et_name=(EditText)findViewById(R.id.et_name);
        et_major=(EditText)findViewById(R.id.et_major);
        et_age=(EditText)findViewById(R.id.et_age);
    }

    @Override
    public void onClick(View v) {
        //获取电话,姓名,专业,年龄
        String phone=et_phone.getText().toString();
        String name=et_name.getText().toString();
        String major=et_major.getText().toString();
        String ageString=et_age.getText().toString();

        try {
             age=Integer.parseInt(ageString);
        }catch(Exception e){
            e.printStackTrace();
        }

        Student stu=new Student();
        stu.setPhone(phone);
        stu.setName(name);
        stu.setMajor(major);
        stu.setAge(age);

        Bundle bundle=new Bundle();
        bundle.putSerializable("student",stu);
        Intent intent=new Intent(MainActivity.this,BActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}

package zhku.edu.exp5_1;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class BActivity extends Activity {
    TextView t_phone,t_name,t_major,t_age;
    Button btn_tel;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);

        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        Student stu=(Student)bundle.getSerializable("student");

            if(stu!=null){
                String tv1=stu.getPhone();
                String tv2=stu.getName();
                String tv3=stu.getMajor();
                int tv4=stu.getAge();

                String tv5=String.valueOf(tv4);

                t_phone=(TextView)findViewById(R.id.tv_phone);
                t_name=(TextView)findViewById(R.id.tv_name);
                t_major=(TextView) findViewById(R.id.tv_major);
                t_age=(TextView)findViewById(R.id.tv_age);

                t_phone.setText(tv1);
                t_name.setText(tv2);
                t_major.setText(tv3);
                t_age.setText(tv5);
    }
        btn_tel=(Button)findViewById(R.id.btn_tel);
        btn_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String phone=t_phone.getText().toString();

                Intent it=new Intent();
                Uri callUri=Uri.parse("tel://"+phone);
                it.setAction(Intent.ACTION_DIAL);
                it.setData(callUri);
                startActivity(it);
            }
        });
    }
}


package zhku.edu.exp5_1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class TelActivity extends Activity {
    TextView tv_tel;
    Student st=new Student();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tel);

        tv_tel=(TextView)findViewById(R.id.tv_tel);

        Intent intent=getIntent();
        //法1;Uri uri=Uri.parse("tel:12345678")可通过uri.toString()输出:tel:12345678
        //法2:Uri uri=Uri.parse("tel://12345678")可通过uri.getHost()输出:12345678
        String out=intent.getData().getHost();
        tv_tel.setText(out);
    }
}
package zhku.edu.exp5_1;
import java.io.Serializable;
public class Student implements Serializable {
    private static final long serialVersionUID=1;
    private String phone;
    private String name;
    private String major;
    private int age;

    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

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

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="zhku.edu.exp5_1.MainActivity">

   <EditText
       android:id="@+id/et_phone"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:textSize="28dp"
       android:hint="电话"/>
    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:hint="姓名 "/>
    <EditText
        android:id="@+id/et_major"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:hint="专业"/>
    <EditText
        android:id="@+id/et_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="28dp"
        android:hint="年龄"/>
    <Button
        android:id="@+id/zerializable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="序列化保存"
        android:layout_marginLeft="10dp"
        android:textSize="25dp"
        android:layout_marginTop="8dp"
        android:background="#00FF00"/>
</LinearLayout>


Activity_b.xml
<?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="我是第二个窗体"
    android:layout_marginTop="15dp"
    android:textSize="18sp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话:"
            android:textSize="25sp"/>
        <TextView
            android:id="@+id/tv_phone"
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="10dp"
        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:textSize="25sp"/>
    <TextView
        android:id="@+id/tv_name"
        android:layout_marginLeft="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"/>
</LinearLayout>
    <LinearLayout
        android:layout_marginTop="10dp"
        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:textSize="25sp"/>
        <TextView
            android:id="@+id/tv_major"
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="10dp"
        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:textSize="25sp"/>
        <TextView
            android:id="@+id/tv_age"
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25dp"/>
    </LinearLayout>
    <Button
        android:id="@+id/btn_tel"
        android:layout_marginTop="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拨打号码 "
        android:textSize="30sp"
        android:background="#ff0000"/>
</LinearLayout>

Activity_tel.xml
<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="您拨打的电话号码是:"
        android:textSize="28dp"/>
    <TextView
        android:id="@+id/tv_tel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="40dp"
        android:textColor="#ff0000"/>
</LinearLayout>

AndriodMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="zhku.edu.exp5_1">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".BActivity"/>

        <activity android:name=".TelActivity">
            <intent-filter>
                <action android:name="android.intent.action.DIAL"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="tel"/>
            </intent-filter>
        </activity>
    </application>

</manifest>


2. ActivitySharedPreference

MainActivity.java
package zhku.edu.exp5_2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Date;
import java.util.Map;

public class MainActivity extends Activity{
    TextView tv_1,tv_2;
    Button btn_clear;
    Map<String,Object> UserInfo;
    int count;
    String date;

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

        getLoginDate();
        initView();
        changeLoginDate();
    }

    public void initView(){
        tv_1=(TextView)findViewById(R.id.tv_count);
        tv_2=(TextView)findViewById(R.id.tv_date);
        btn_clear=(Button)findViewById(R.id.btn_clear);

        tv_1.setText((count+1)+"");
        tv_2.setText(date);

        btn_clear.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                MyUtils.clearUserInfo(MainActivity.this);
                tv_1.setText("0");
                tv_2.setText("无");
            }
        });
    }

    public void getLoginDate(){//获取登录信息
        UserInfo=MyUtils.getUserInfo(this);
        count=(Integer)UserInfo.get(MyUtils.NUM);
        date=(String)UserInfo.get(MyUtils.TIME);
    }

    public void changeLoginDate(){//修改登录信息
        MyUtils.saveUserInfo(this,count+1,new Date());
    }
}
MyUtils.java
package zhku.edu.exp5_2;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;


public class MyUtils {// 创建工具类封装
    public static final String NAME="data";
    public static final String NUM="count";
    public static final String TIME="date";

    public static void saveUserInfo(Context context, int count, Date date){
//保存Preference
        SharedPreferences sp = context.getSharedPreferences(NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt(NUM, count);//更新登录次数
        DateFormat df = new SimpleDateFormat("yyyy‐MM‐dd hh:mm:ss");//更新登录时间
        edit.putString(TIME, df.format(date));//格式化当前日期,以字符串保存
        edit.commit();
    }
    public static Map<String, Object> getUserInfo(Context context) {
        SharedPreferences sp = context.getSharedPreferences(NAME,
                Context.MODE_PRIVATE);
        int count = sp.getInt(NUM, 0);//获取登录次数
        String date = sp.getString(TIME, "无");//获取上次登录时间
        Map<String, Object> data = new HashMap<String, Object>();
        data.put(NUM, new Integer(count));
        data.put(TIME, date);
        return data;//以Map 数据返回
    }
    public static void clearUserInfo(Context context) {
        SharedPreferences sp = context.getSharedPreferences(NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.clear();
        edit.commit();
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值