程序笔记   发布时间:2022-07-05  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了第11 12 周大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<?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_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入你想写入的内容:"
        android:layout_margin="20dp"/>
    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入"
        android:layout_margin="5dp"
        android:onClick="click1"/>
    <EditText
        android:id="@+id/et_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="显示读取的内容"
        android:layout_margin="20dp"/>
    <Button
        android:id="@+id/bt_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取"
        android:layout_margin="5dp"
        android:onClick="click2"/>
</LinearLayout>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.example.zy;
 
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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.bt_1);
        findViewById(R.id.bt_2);
        EditText editText1=findViewById(R.id.et_1);
        EditText editText2=findViewById(R.id.et_2);
    }
    public void click1(View view){
        EditText editText1=findViewById(R.id.et_1);
        EditText editText2=findViewById(R.id.et_2);
        String fileName="data.txt";
        String content=editText1.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(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
    }
 public void click2(View view){
        EditText editText2=findViewById(R.id.et_2);
        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();
                }
            }
        }
        Toast.makeText(MainActivity.this,"读取成功",Toast.LENGTH_SHORT).show();
        editText2.setText(content);
    }
}

  

第11 12 周

 2.使用sharedpreference实现记住密码功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?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">
<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="账号:"
        android:textSize="25sp"
        android:textColor="#000"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="10dp"/>
    <EditText
        android:id="@+id/et_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名:"
        android:layout_marginTop="40dp"
        android:textSize="25sp"/>
</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="密码:"
        android:textSize="25sp"
        android:textColor="#000"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="10dp"/>
    <EditText
        android:id="@+id/et_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:layout_marginTop="15dp"
        android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:textSize="20sp"
        android:layout_marginTop="15dp"/>
    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自动登录"
        android:textSize="20sp"
        android:layout_marginTop="15dp"/>
    <Button
        android:id="@+id/bt_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陆"
        android:textSize="20sp"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="35dp"
        android:onClick="clickQQ"
        tools:ignore="OnClick" />
</LinearLayout>
</LinearLayout>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.example.dierti;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    private EditText yonghuming,mima;
    private CheckBox box;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        yonghuming=findViewById(R.id.et_3);
        mima=findViewById(R.id.et_4);
        box=findViewById(R.id.cb_1);
        load();
    }
 
    public void clickQQ(View view){
        String yhm=yonghuming.getText().toString();
        String mm=mima.getText().toString();
        if (box.isChecked()){
            SharedPreferences sp=getSharedPreferences("info",MODE_PRIVATE);
            SharedPreferences.Editor et=sp.edit();
            et.putString("YHM",yhm);
            et.putString("MM",mm);
            et.commit();
            Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
        }
    }
 
    private void load() {
        SharedPreferences sp=getSharedPreferences("info",MODE_PRIVATE);
        String yhmString=sp.getString("YHM","");
        String mmString=sp.getString("MM","");
        yonghuming.setText(yhmString);
        mima.setText(mmString);
    }
}

  

第11 12 周

大佬总结

以上是大佬教程为你收集整理的第11 12 周全部内容,希望文章能够帮你解决第11 12 周所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: