第十一 十二周
<?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:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内部存储:" android:textSize="38sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <EditText android:id="@+id/et_out" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:gravity="center" android:hint="请输入要保存的内容" /> <Button android:id="@+id/btn_output" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="click_output" android:text="写入" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <EditText android:id="@+id/et_in" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:gravity="center" android:hint="输出已保存的内容" /> <Button android:id="@+id/btn_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click_input" android:text="读取" /> </LinearLayout> </LinearLayout>
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { private EditText et_out, et_in; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //findViewByiId 初始化 } private void initView() { //findViewByiId 初始化 et_out = findViewById(R.id.et_out); et_in = findViewById(R.id.et_in); } //findViewByiId 初始化 public void click_output(View view) { //写入输入框内的字符串 String fileName = "data.txt"; //创建储存文件 String content = et_out.getText().toString(); //获取储存字符串 FileOutputStream fos = null; //创建创建 FilOutputStream 对象 使用其 write() 方法储存数据 try { fos = openFileOutput(fileName, MODE_PRIVATE); //打开 并获取 输出流对象 (文件名, 操作模式) fos.write(content.getBytes()); //wirte()方法 将数据写入文件 } catch (Exception e) { e.printStackTrace(); //抛出异常 } finally { try { if (fos != null) { fos.close(); //关闭输出流 } } catch (IOException e) { e.printStackTrace(); //抛出异常 } } Toast.makeText(MainActivity.this, content + "写入成功!", Toast.LENGTH_SHORT).show(); } //写入输入框内的字符串 public void click_input(View view) { String content = ""; //创建 空字符串变量 获取 已保存的 字符串 FileInputStream fis = null; //创建 FileInputStream 对象 try { fis = openFileInput("data.txt"); //获得文件输入流对象 byte[] buffer = new byte[fis.available()]; //创建缓冲区,并获取文件长度 fis.read(buffer); //将文件内容读取到 buffer 缓冲区 content = new String(buffer); //转换成字符串 } catch (Exception e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); //关闭输入流 } } catch (IOException e) { e.printStackTrace(); } } et_in.setText(content); Toast.makeText(MainActivity.this, content + "读取成功!", Toast.LENGTH_SHORT).show(); } //输出字符串至 输出框 }
package com.example.myapplication; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.content.DialogInterface.OnClickListener; import android.content.SharedPreferences.Editor; import android.text.TextUtils; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.CheckResult; public class MainActivity extends Activity implements android.view.View.OnClickListener , CompoundButton.OnCheckedChangeListener { 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=saveQQ.getUserInfo(this); if(userInfo!=null){ et_account.setText(userInfo.get("account")); et_password.setText(userInfo.get("password")); } } private void initView() { // TODO Auto-generated method stub et_account=(EditText)findViewById(R.id.et1); et_password=(EditText)findViewById(R.id.et2); btn_login=(Button)findViewById(R.id.bt1); btn_login.setOnClickListener(this); CheckBox cb1=findViewById(R.id.cb1); cb1.setOnCheckedChangeListener(this); CheckBox cb2=findViewById(R.id.cb2); cb2.setOnCheckedChangeListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.bt1: 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(); } } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { switch (compoundButton.getId()){ case R.id.cb1 : String account=et_account.getText().toString().trim(); String password=et_password.getText().toString(); boolean isSaveSuccess=saveQQ.saveUserInfo(this, account, password); if(isSaveSuccess&&b){ Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); et_account.setText(null); et_password.setText(null); } break; case R.id.cb2: account=et_account.getText().toString().trim(); 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; } if(b) Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); else{ Toast.makeText(this, "请登录", Toast.LENGTH_SHORT).show(); } } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号" android:layout_marginLeft="50dp" android:layout_marginTop="10dp" /> <EditText android:id="@+id/et1" android:layout_width="300dp" android:layout_height="wrap_content" android:hint="请输入用户名" android:layout_toRightOf="@id/tv1"/> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:text="密码" android:layout_marginTop="10dp" android:layout_below="@id/et1"/> <EditText android:id="@+id/et2" android:layout_width="300dp" android:layout_height="wrap_content" android:hint="请输入密码" android:layout_below="@id/et1" android:layout_toRightOf="@id/tv2"/> <Button android:id="@+id/bt1" android:layout_width="74dp" android:layout_height="35dp" android:layout_below="@id/cb1" android:layout_marginLeft="280dp" android:layout_marginTop="-35dp" android:background="#DF0719" android:text="登录" android:textColor="#000000" /> <CheckBox android:id="@+id/cb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:layout_below="@id/et2" android:layout_marginTop="10dp" android:layout_marginLeft="50dp"/> <CheckBox android:id="@+id/cb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动登录" android:layout_below="@id/et2" android:layout_toRightOf="@id/cb1" android:layout_marginTop="10dp" android:layout_marginLeft="20dp"/> </RelativeLayout>
package com.example.myapplication; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; public class saveQQ { public static boolean saveUserInfo(Context context,String account,String password ){ SharedPreferences sp=context.getSharedPreferences("data", context.MODE_PRIVATE); SharedPreferences.Editor editor=sp.edit(); editor.putString("Username", account); editor.putString("pwd", password); editor.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; } }