简体   繁体   English

Android sharedPreferences定向问题

[英]Android sharedPreferences orientation problems

I have problem to save seekbar state. 我在保存seekbar状态时遇到问题。 Basically I put seekbar only in landscape mode and try to use sheredPreferences to save it. 基本上,我只将搜寻栏放在横向模式下,并尝试使用sheredPreferences进行保存。

Code is working well when seekbar is in above orientation but I need it only in landscape mode. 当seekbar处于上述方向时,代码运行良好,但我仅在横向模式下需要它。 I try to use something like this in onCreate method: 我尝试在onCreate方法中使用类似的方法:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

Button  btncount;
TextView e1, e2;
SeekBar zip;

int txt;

double x , y = 22.54, z;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    zip = (SeekBar)findViewById(R.id.zip);
    btncount = (Button)findViewById(R.id.btncount);
    e1 = (TextView)findViewById(R.id.e1);
    e2 = (TextView)findViewById(R.id.e2);

    btncount.setOnClickListener(this);

    SharedPreferences settings = getSharedPreferences("mypref", 0);
    //save in above orientation


    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
        case Configuration.ORIENTATION_LANDSCAPE:
            zip.setOnSeekBarChangeListener(actionseekBar);
            int mProgress = settings.getInt("seekname", 0);
            //save only in landscape orientation
            zip.setProgress(mProgress);
            e1.setText(settings.getString("ValueY", "0"));
            e2.setText(settings.getString("ValueZ", "0"));

            break;
        case Configuration.ORIENTATION_PORTRAIT:
            e1.setText(settings.getString("ValueY", "0"));
            e2.setText(settings.getString("ValueZ", "0"));
            break;
    }
}

@Override
protected void onStop(){
    super.onStop();

    SharedPreferences settings = getSharedPreferences("mypref", 0);
    SharedPreferences.Editor editor = settings.edit();


    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
        case Configuration.ORIENTATION_LANDSCAPE:
            editor.putString("ValueY", e1.getText().toString());
            editor.putString("ValueZ", e2.getText().toString());
            editor.putInt("seekname", zip.getProgress());
            break;
        case Configuration.ORIENTATION_PORTRAIT:
            editor.putString("ValueY", e1.getText().toString());
            break;
    }
    editor.commit();
}

private final SeekBar.OnSeekBarChangeListener actionseekBar = new SeekBar.OnSeekBarChangeListener(){
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        txt = progress;

        e2.setText(String.valueOf(txt));

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
};

@Override
public void onClick(View v) {
    if(v.getId()== R.id.btncount){
        counter(v);
    }
}

public void counter(View v){

    z = Double.parseDouble(e2.getText().toString());

    x = y + z;

    e1.setText(String.valueOf(x));
}

} }

It's not working, can anyone help me with this? 它没有用,有人可以帮我吗?

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {

Button btncount;
TextView e1, e2;
SeekBar zip;

int txt;

double x, y = 22.54, z;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
        case Configuration.ORIENTATION_LANDSCAPE:
            e1 = (TextView) findViewById(R.id.e1);
            zip = (SeekBar) findViewById(R.id.zip);
            btncount = (Button) findViewById(R.id.btncount);
            e2 = (TextView) findViewById(R.id.e2);

            btncount.setOnClickListener(this);

            zip.setOnSeekBarChangeListener(actionseekBar);

            //save only in landscape orientation
            SharedPreferences settings = getSharedPreferences("mypref", 0);

            if(settings != null) {

                int mProgress = settings.getInt("seekname", 0);

                zip.setProgress(mProgress);
                e1.setText(settings.getString("ValueY", "0"));

            }else{
                Toast.makeText(MainActivity.this, "No saved objects", Toast.LENGTH_SHORT).show();
            }

            break;
        case Configuration.ORIENTATION_PORTRAIT:

            break;
    }
}


private final SeekBar.OnSeekBarChangeListener actionseekBar = new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        txt = progress;

        e1.setText(String.valueOf(txt));

        SharedPreferences settings = getSharedPreferences("mypref", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("ValueY", e1.getText().toString());
        editor.putInt("seekname", zip.getProgress());
        editor.apply();

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
};

@Override
public void onClick(View v) {
    if (v.getId() == R.id.btncount) {
        counter(v);
    }
}

public void counter(View v) {

    z = Double.parseDouble(e1.getText().toString());

    x = y + z;

    e2.setText(String.valueOf(x));
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM