作业
1.
package com.example.charp1;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click1(View view) {
EditText editText1 = (EditText) findViewById(R.id.et1);
EditText editText2 = (EditText) findViewById(R.id.et2);
String filename = "data.txt";
String context = editText1.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(filename, MODE_PRIVATE);
fos.write(context.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
}
public void click2(View view) {
EditText editText2 = (EditText) findViewById(R.id.et2);
String context = "";
FileInputStream fis = null;
try {
fis = openFileInput("data.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
context = new String(buffer);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
editText2.setText(context);
}
}
<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"
tools:context=".MainActivity"
android:orientation="vertical" >
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="输入你想写入的内容"
/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入"
android:onClick="click1"/>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示读取的内容"
/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取"
android:onClick="click2"/>
</LinearLayout>
2.
<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:" />
<EditText
android:id="@+id/ed3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入用户名" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:" />
<EditText
android:id="@+id/ed4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入密码" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:onClick="click1"/>
</LinearLayout>
</LinearLayout>
package com.example.charp2;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.Menu;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static boolean saveUserInfo(Context context, String account,
String password){
SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("userName", account);
edit.putString("pwd", password);
edit.commit();
return true;
}
public static Map<String, String> getUserfo(Context context){
SharedPreferences sp= context.getSharedPreferences("data", Context.MODE_PRIVATE);
String account=sp.getString("userName", null);
String password =sp.getString("pwd", null);
Map<String,String> userMap= new HashMap<String,String>();
userMap.put("account", account);
userMap.put("password", password);
return userMap;
}
}

2.
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<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/et_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入用户名" />
</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/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入密码" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录" />
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="登录" />
</LinearLayout>
</LinearLayout>
package com.example.qq; import android.content.Context; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class FileSaveQQ { public static boolean saveUserInfo(Context context, String account, String password ) { FileOutputStream fos = null; try{ fos =context.openFileOutput("data.txt",Context.MODE_PRIVATE); fos.write((account+":"+password).getBytes()); return true; }catch (Exception e){ e.printStackTrace(); return false; }finally { try{ if (fos !=null){ fos.close(); } }catch (IOException e){ e.printStackTrace(); } } } public static Map<String, String> getUserInfo(Context context){ String content=""; FileInputStream fis= null; try { fis=context.openFileInput("data.txt"); byte[] buffer =new byte[fis.available()]; fis.read(buffer); content=new String(buffer); Map<String,String> userMap = new HashMap<String, String>(); String[] infos = content.split(":"); userMap.put("account",infos[0]); userMap.put("password",infos[1]); return userMap; }catch (Exception e){ e.printStackTrace(); return null; } finally { try { if (fis!=null){ fis.close(); } }catch (IOException e){ e.printStackTrace(); } } } }
package com.example.qq; import android.content.Context; import android.content.SharedPreferences; import java.util.HashMap; import java.util.Map; public class SPSaveQQ { public static boolean saveUserInfo(Context context,String account,String password){ SharedPreferences sp= context.getSharedPreferences("data",Context.MODE_PRIVATE); SharedPreferences.Editor edit =sp.edit(); edit.putString("Username",account); edit.putString("pwd",password); edit.commit(); return true; } public static Map<String,String> getUserInfo(Context context){ SharedPreferences sp =context.getSharedPreferences("data",Context.MODE_PRIVATE); String account=sp.getString("username",null); String password=sp.getString("pwd",null); Map<String,String> userMap= new HashMap<String, String>(); userMap.put("account",account); userMap.put("password",password); return userMap; } }
package com.example.qq; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.Map; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText et_account; private EditText et_password; private Button btn_login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); Map<String, String> userInfo = FileSaveQQ.getUserInfo(this); if (userInfo != null) { et_account.setText(userInfo.get("account")); et_password.setText(userInfo.get("password")); } } private void initView() { et_account = findViewById(R.id.et_account); et_password = findViewById(R.id.et_password); btn_login = findViewById(R.id.btn_login); btn_login.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_login: String account = et_account.getText().toString().trim(); String password = et_password.getText().toString(); if (TextUtils.isEmpty(account)) { Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(password)) { Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show(); return; } Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); boolean isSaveSuccess = FileSaveQQ.saveUserInfo(this, account, password); if (isSaveSuccess) { Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); } break; } } }


浙公网安备 33010602011771号