第十一十二周作业

 

 

<?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"
tools:context=".MainActivity"
android:orientation="vertical">

<EditText
android:id="@+id/et_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存的数据" />

<Button
android:id="@+id/bt_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click1"
android:text="保存数据"/>
<EditText
android:id="@+id/et_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="@+id/bt_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click2"
android:text="读取数据"/>
</LinearLayout>






package com.example.badboy.aaa;

import android.support.v7.app.ActionBarActivity;
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 ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt_1);
findViewById(R.id.bt_2);
findViewById(R.id.bt_1);
findViewById(R.id.bt_2);
}
public void onClick(View view) {
switch (view.getId()){
case R.id.bt_1:
String fileName="data.txt";
String content=((EditText)findViewById(R.id.et_1)).getText().toString();
FileOutputStream fos=null;
try{
fos=openFileOutput(fileName,MODE_PRIVATE);
fos.write(content.getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case R.id.bt_2:
content = "";
FileInputStream fis=null;
try{
fis=openFileInput("data.txt");
byte[] buffer=new byte[fis.available()];
fis.read(buffer);
content=new String(buffer);
((EditText)findViewById(R.id.et_2)).setText(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
try{
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
}



 

 

<?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:textSize="25dp"
android:layout_above="@+id/et2"
android:layout_alignLeft="@+id/tv2"
android:layout_alignStart="@+id/tv2" />
<EditText
android:id="@+id/et1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:layout_toRightOf="@id/tv1"
android:text="11515" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="25dp"
android:layout_margin="10dp"
android:layout_below="@id/tv1"/>
<EditText
android:id="@+id/et2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:layout_toRightOf="@id/tv2"
android:layout_below="@id/et1"
android:layout_marginTop="10dp"
android:text="3165" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv2"
android:onClick="cbclick"/>
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:layout_toRightOf="@id/cb1"
android:layout_below="@id/tv2"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv2"
android:layout_toRightOf="@id/tv3"/>
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"
android:layout_toRightOf="@id/cb2"
android:layout_below="@id/tv2"
android:textSize="20dp" />
<Button
android:id="@+id/bt1"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="登录"
android:layout_toRightOf="@id/tv4"
android:layout_below="@id/et2"
android:onClick="btclick"/>



</RelativeLayout>


package com.example.badboy.bbb;

import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btclick(View view){
String et1=((EditText)(findViewById(R.id.et1))).getText().toString();
String et2=((EditText)(findViewById(R.id.et2))).getText().toString();
SharedPreferences sp=getSharedPreferences("cdate",MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("zh", et1);
editor.putString("mm", et2);
editor.commit();
}
public void cbclick(View view){
SharedPreferences sp=getSharedPreferences("cdate", MODE_PRIVATE);
String zh=sp.getString("zh", "");
String mm=sp.getString("mm", "");
EditText et1=(EditText)findViewById(R.id.et1);
EditText et2=(EditText)findViewById(R.id.et2);
et1.setText(zh);
et2.setText(mm);
}
}





posted @ 2021-11-02 23:38  高杨翔  阅读(13)  评论(0编辑  收藏  举报