zoukankan      html  css  js  c++  java
  • 4月14日学习日志

    今天学习了SharedPreferences保存。

    布局文件为:

    <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=".MyActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户登陆" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="请输入用户名" />
    
        <EditText
            android:id="@+id/editname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="用户名" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请输入密码" />
    
        <EditText
            android:id="@+id/editpasswd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="密码"
            android:inputType="textPassword" />
    
        <Button
            android:id="@+id/btnlogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录" />
    </LinearLayout>

    简单SP工具类为:

    public class SharedHelper {
    
        private Context mContext;
    
        public SharedHelper() {
        }
    
        public SharedHelper(Context mContext) {
            this.mContext = mContext;
        }
    
    
        //定义一个保存数据的方法
        public void save(String username, String passwd) {
            SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("username", username);
            editor.putString("passwd", passwd);
            editor.commit();
            Toast.makeText(mContext, "信息已写入SharedPreference中", Toast.LENGTH_SHORT).show();
        }
    
        //定义一个读取SP文件的方法
        public Map<String, String> read() {
            Map<String, String> data = new HashMap<String, String>();
            SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
            data.put("username", sp.getString("username", ""));
            data.put("passwd", sp.getString("passwd", ""));
            return data;
        }
    }

    主要代码为:

    public class MainActivity extends AppCompatActivity {
    
        private EditText editname;
        private EditText editpasswd;
        private Button btnlogin;
        private String strname;
        private String strpasswd;
        private SharedHelper sh;
        private Context mContext;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mContext = getApplicationContext();
            sh = new SharedHelper(mContext);
            bindViews();
        }
    
        private void bindViews() {
            editname = (EditText)findViewById(R.id.editname);
            editpasswd = (EditText)findViewById(R.id.editpasswd);
            btnlogin = (Button)findViewById(R.id.btnlogin);
            btnlogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    strname = editname.getText().toString();
                    strpasswd = editpasswd.getText().toString();
                    sh.save(strname,strpasswd);
                }
            });
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            Map<String,String> data = sh.read();
            editname.setText(data.get("username"));
            editpasswd.setText(data.get("passwd"));
        }
    }
  • 相关阅读:
    终于找到一个在IE8下可以使用搜索栏的输入法了
    在psp中播放电脑上的flv文件
    屏蔽红警3强制升级
    在windows7中安装了office了
    Flv视频编辑软件FlvEditor
    射杀恋人之日
    USB口不够用了
    年底了,游戏大作连连
    又一个好用的xbox360手柄驱动
    Windows7体验小记
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14909848.html
Copyright © 2011-2022 走看看