「已注销」 2016-09-23 06:49 采纳率: 0%
浏览 844

使用SharedPreferences读取edittext值得时候出现一点小问题求解决

public void WriteUserEditInfo() {
SharedPreferences userdetect = context.getSharedPreferences("hotspots_info", 0);
SharedPreferences.Editor editor = userdetect.edit();
editor.putString("name", edt_hotspots_name.getText().toString());
editor.putString("password", edt_hotspots_password.getText().toString());
editor.commit();
}


这是我的函数 在两个item中各有一个不一样的edittext另一个分别设置成viewgone 然后读取的时候也是正常读取的 然后我发现了一个问题

    switch (position) {
        case 0:
            viewHolder.edt_hotspots_name.setText(name);
            viewHolder.edt_input_1.setVisibility(View.GONE);
            viewHolder.edt_input_2.setVisibility(View.GONE);
            viewHolder.edt_input_3.setVisibility(View.GONE);
            viewHolder.edt_input_4.setVisibility(View.GONE);
            viewHolder.edt_hotspots_password.setVisibility(View.GONE);
            viewHolder.edt_hotspots_name.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    viewHolder.WriteUserEditInfo();
                }
            });


            break;
        case 1:

            break;
        case 2:
            textView.setText(JukeSkin.getInstance().getStringResource("network_mode"));

            viewHolder.edt_hotspots_password.setVisibility(View.GONE);
            viewHolder.edt_hotspots_name.setVisibility(View.GONE);
            break;
        case 3:
            viewHolder.edt_hotspots_password.setText(password);

            viewHolder.edt_hotspots_password.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    viewHolder.WriteUserEditInfo_password();
                }
            });

            break;
        case 4:

            break;
    }

如果我只写一个函数WriteUserEditInfo
SharedPreferences userdetect = context.getSharedPreferences("hotspots_info", 0);
SharedPreferences.Editor editor = userdetect.edit();
editor.putString("name", edt_hotspots_name.getText().toString());
editor.putString("password", edt_hotspots_password.getText().toString());
editor.putString("edt_input1", edt_input_1.getText().toString());
editor.putString("edt_input2", edt_input_2.getText().toString());
editor.putString("edt_input3", edt_input_2.getText().toString());
editor.putString("edt_input4", edt_input_4.getText().toString());
editor.commit();

                    然后edittext 
        public void afterTextChanged(Editable s)的时候调用只会保存最后一次输入的那个edittext的信息 其他的并不会被保存

                    然后我就写了两个函数分别从两个XML文件中读取数据,但是edittext多的时候实在很不方便。。所以想请教一下这里要怎么解决?之前写在activity里面是可疑的,不知道哪里出了一点问题。。
  • 写回答

1条回答

  • dabocaiqq 2016-09-24 15:27
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题