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

发布于:2021-07-20 04:30:45

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

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">

android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:hint="电话"/>
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:hint="姓名 "/>
android:id="@+id/et_major"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:hint="专业"/>
android:id="@+id/et_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:hint="年龄"/>
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"/>





Activity_b.xml

android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是第二个窗体"
android:layout_marginTop="15dp"
android:textSize="18sp"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电话:"
android:textSize="25sp"/>
android:id="@+id/tv_phone"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"/>

android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="25sp"/>
android:id="@+id/tv_name"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"/>

android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="专业:"
android:textSize="25sp"/>
android:id="@+id/tv_major"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"/>

android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="25sp"/>
android:id="@+id/tv_age"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"/>

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"/>



Activity_tel.xml

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="您拨打的电话号码是:"
android:textSize="28dp"/>
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"/>



AndriodMainfest.xml

package="zhku.edu.exp5_1">

android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
























2. Activity的SharedPreference



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 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 getUserInfo(Context context) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
int count = sp.getInt(NUM, 0);//获取登录次数
String date = sp.getString(TIME, "无");//获取上次登录时间
Map data = new HashMap();
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();
}
}











相关推荐

最新更新

猜你喜欢