第十一 十二周作业
1.
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.content.SharedPreferences; import android.util.Log; 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 editText=findViewById(R.id.et1); String filename="data.txt"; String content=editText.getText().toString(); FileOutputStream fos=null; try { fos=openFileOutput(filename, MODE_PRIVATE); fos.write(content.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 editText=findViewById(R.id.et2); String content=""; FileInputStream fis=null; try { fis=openFileInput("data.txt"); byte[] buffer=new byte[fis.available()]; fis.read(buffer); content=new String(buffer); } catch (Exception e) { e.printStackTrace(); }finally{ if(fis!=null) try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } editText.setText(content); }
<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" tools:context=".MainActivity" android:orientation="vertical" > <EditText android:id="@+id/et1" android:layout_width="280dp" android:layout_height="wrap_content" android:hint="输入你想写入的内容" android:textSize="70px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click1" android:text="写入" android:textSize="35dp"/> <EditText android:id="@+id/et2" android:layout_width="280dp" android:layout_height="wrap_content" android:hint="显示读取的内容" android:textSize="70px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click2" android:text="读取" android:textSize="35dp"/> </LinearLayout>
2.
import java.io.File; 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; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements android.view.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() { // TODO Auto-generated method stub et_account=(EditText)findViewById(R.id.et_account); et_password=(EditText)findViewById(R.id.et_password); btn_login=(Button)findViewById(R.id.btn_login); btn_login.setOnClickListener(this); CheckBox cb1=findViewById(R.id.cb_1); cb1.setOnClickListener(this); CheckBox cb2=findViewById(R.id.cb_2); cb2.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.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(); } } public void onCheckedChanged(CompoundButton compoundButton, boolean b) { switch (compoundButton.getId()){ case R.id.cb_1 : String account=et_account.getText().toString().trim(); String password=et_password.getText().toString(); boolean isSaveSuccess=FileSaveQQ.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.cb_2: 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(); } } } }
import android.content.Context; import android.content.SharedPreferences; import java.util.HashMap; import java.util.Map; class FileSaveQQ{ private static String password; public static boolean saveUserInfo(Context context, String account, String password) { FileSaveQQ.password = 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("useName",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; } }
<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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="账号" android:textColor="#000" android:textSize="20sp" /> <EditText android:id="@+id/et_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:hint="请输入用户名" android:background="@null" android:padding="10dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:orientation="horizontal" > <TextView android:id="@+id/tv_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="密码" android:textColor="#000" android:textSize="20sp" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@null" android:hint="请输入密码" android:inputType="textPassword" android:padding="10dp" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="100dp" android:orientation="horizontal" > <CheckBox android:id="@+id/cb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="3dp" android:layout_marginTop="19dp" android:text="记住密码" android:textSize="20sp" /> <CheckBox android:id="@+id/cb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="140dp" android:layout_marginTop="19dp" android:text="自动登录" android:textSize="20sp" /> <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="280dp" android:layout_marginTop="15dp" android:background="#2481BE" android:text="登录" android:textColor="@android:color/white" android:textSize="20sp" /> </RelativeLayout> </LinearLayout>
posted on 2021-11-04 20:10 chenyulin11 阅读(11) 评论(0) 编辑 收藏 举报