第十一、十二周作业

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_xieru"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入你想写入的内容"
        android:textSize="30dp"
        android:layout_marginTop="20dp"/>

    <Button
        android:id="@+id/bt_xieru"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click1"
        android:text="写入"
        android:textSize="30dp"
        android:layout_marginLeft="20dp" />

    <EditText
        android:id="@+id/et_duqu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="显示读取的内容"
        android:textSize="30dp"
        android:layout_marginTop="20dp"/>

    <Button
        android:id="@+id/bt_duqu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click2"
        android:text="读取"
        android:textSize="30dp"
        android:layout_marginLeft="20dp" />

</LinearLayout>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text12_1);
    }

    public void click1(View view){
        EditText editText1=findViewById(R.id.et_xieru);
        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_LONG).show();

    }
    public void click2(View view){
        EditText editText2=findViewById(R.id.et_duqu);
        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);
    }

 

public class MainActivity extends AppCompatActivity {

    private EditText edit_user,edit_psw;
    private Button bt_denglu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text12_1);

        edit_user = findViewById(R.id.et_user);
        edit_psw =  findViewById(R.id.et_psw);
        bt_denglu= findViewById(R.id.bt_denglu);

        bt_denglu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = getSharedPreferences("share.txt", MODE_PRIVATE).edit();
                editor.putString("name", edit_user.getText().toString());
                editor.putString("password",edit_psw.getText().toString());
                editor.commit();
            }
        });
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="账号"
        android:textSize="30dp"
        android:layout_marginTop="150dp"
        android:layout_marginLeft="40dp" />

    <EditText
        android:id="@+id/et_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_user"
        android:layout_marginTop="150dp"
        android:layout_marginRight="40dp"/>

    <TextView
        android:id="@+id/tv_psw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码"
        android:textSize="30dp"
        android:layout_below="@+id/et_user"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="30dp"/>

    <EditText
        android:id="@+id/et_psw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_psw"
        android:layout_below="@+id/et_user"
        android:layout_marginTop="30dp"
        android:layout_marginRight="40dp"/>

    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_psw"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="20dp"
        android:text="记住密码"
        android:textSize="15sp" />

    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/cb_1"
        android:layout_alignBottom="@+id/cb_1"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/cb_1"
        android:text="自动登录"
        android:textSize="15sp" />

    <Button
        android:id="@+id/bt_denglu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/cb_2"
        android:layout_alignBottom="@+id/cb_2"
        android:layout_alignRight="@+id/et_psw"
        android:text="登录" />

</RelativeLayout>

 

 

posted @ 2021-11-04 18:28  计算机1905方骏  阅读(8)  评论(0编辑  收藏  举报