2 sld22525482 sld22525482 于 2016.09.23 14:49 提问

使用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
dabocaiqq   2016.09.24 23:27
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!