Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:使用多个编辑文本保存首选项_Java_Android_Sharedpreferences_Preferences - Fatal编程技术网

Java Android:使用多个编辑文本保存首选项

Java Android:使用多个编辑文本保存首选项,java,android,sharedpreferences,preferences,Java,Android,Sharedpreferences,Preferences,我试图保存用户在3个editText字段(editText1、editText2、editText3)中输入的值,以便在应用程序重新启动后显示在正确的字段中。 问题:重新启动后,只有保存在中的editText3的最后一个值会填充到3个字段中。 为了保存和重新加载特定字段的值,有什么解决方法 解决了 public class MainActivity extends AppCompatActivity { int sum = 0; int ects = 0; float mark = 0; Num

我试图保存用户在3个editText字段(editText1、editText2、editText3)中输入的值,以便在应用程序重新启动后显示在正确的字段中。 问题:重新启动后,只有保存在中的editText3的最后一个值会填充到3个字段中。 为了保存和重新加载特定字段的值,有什么解决方法

解决了

public class MainActivity extends AppCompatActivity {
int sum = 0;
int ects = 0;
float mark = 0;
NumberFormat numberFormat = new DecimalFormat("0.00");
Button button1;
TextView textView1;
EditText editText1;
EditText editText2;
EditText editText3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 = (Button)findViewById(R.id.button1);
    textView1 = (TextView)findViewById(R.id.textView1);
    editText1 = (EditText)findViewById(R.id.editTextGDI);
    editText2 = (EditText)findViewById(R.id.editTextGDW);
    editText3 = (EditText)findViewById(R.id.editTextProgrammieren1);
    loadSavedPreferences();

    button1.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v){

                    ViewGroup group = (ViewGroup)findViewById(R.id.activity_main);
                    for (int i = 0, count = group.getChildCount(); i < count; ++i) {
                        View view = group.getChildAt(i);
                        if (view instanceof EditText) {
                            if(view.equals(editText1) && !(editText1.getText().toString().matches(""))){ //GDI
                                System.out.println("edittext1");
                                ects += 5;
                                try{ sum += Integer.parseInt(editText1.getText().toString()) *5; } catch(NumberFormatException n){}
                            }
                            else if(view.equals(editText2)&& !(editText2.getText().toString().matches(""))){ //GDW
                                System.out.println("edittext2");
                                ects += 5;
                                try{ sum += Integer.parseInt(editText2.getText().toString()) *5; } catch(NumberFormatException n){}
                            }
                            else if(view.equals(editText3)&& !(editText3.getText().toString().matches(""))){
                                System.out.println("edittext3");
                                ects += 7;
                                try{ sum += Integer.parseInt(editText3.getText().toString()) *7; } catch(NumberFormatException n){}
                            }
                        }
                    }

                    mark = (float)sum / (float)ects;
                    textView1.setText(String.valueOf(numberFormat.format(mark)));
                    savePreferences("1", editText1.getText().toString());
                    savePreferences("2", editText2.getText().toString());
                    savePreferences("3", editText3.getText().toString());

                    sum = 0;
                    ects = 0;
                    mark = 0;
                }
            }
    );
}

private void loadSavedPreferences() {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    editText1.setText(sharedPreferences.getString("1", "YourName"));
    editText2.setText(sharedPreferences.getString("2", "YourName"));
    editText3.setText(sharedPreferences.getString("3", "YourName"));
}

private void savePreferences(String key, String value) {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}
}
public类MainActivity扩展了AppCompatActivity{
整数和=0;
int ects=0;
浮点数=0;
NumberFormat NumberFormat=新的十进制格式(“0.00”);
按钮1;
文本视图文本视图1;
编辑文本编辑文本1;
编辑文本编辑文本2;
编辑文本编辑文本3;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(按钮)findViewById(R.id.button1);
textView1=(TextView)findViewById(R.id.textView1);
editText1=(EditText)findViewById(R.id.editTextGDI);
editText2=(EditText)findViewById(R.id.editTextGDW);
editText3=(EditText)findViewById(R.id.editTextProgrammieren1);
loadSavedReferences();
button1.setOnClickListener(
新建按钮。OnClickListener(){
公共void onClick(视图v){
ViewGroup group=(ViewGroup)findViewById(R.id.activity\u main);
for(int i=0,count=group.getChildCount();i
您需要为每个保存的文本使用不同的键:

savePreferences("storedName", editText1.getText().toString());
savePreferences("storedName2", editText2.getText().toString());
savePreferences("storedName3", editText3.getText().toString());
然后,检索每个文本

private void loadSavedPreferences() {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    editText1.setText(sharedPreferences.getString("storedName", "YourName"));
    editText2.setText(sharedPreferences.getString("storedName2", "YourName"));
    editText3.setText(sharedPreferences.getString("storedName3", "YourName"));
}

请小心,因为getString中的第二个参数(“YourName”)是默认值,以防您没有使用指定的键参数保存任何文本。

通过将