微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

保持 Spinner Android Studio 的最后注册状态

如何解决保持 Spinner Android Studio 的最后注册状态

我正在开发一个应用程序,它有一些开关按钮,其中有 SharedPreferences 以便在应用程序关闭和重新打开时,之前激活的按钮保持不变。

MainActivity 类中的 SharedPreferences:

private Spinner spipol;
    private Switch quim,fil,fis,tri,esp,engl,inf,eti,reli,est,pol,dib,edf,mate;
    private SharedPreferences sharedPreferences,sharfil,sharquim,shardfis,shartrig,sharesp,sharemater,sharing,sharinf,shareti,sharrel,sharest,sharpol,sharedf;
    public static final String ex1 = "switch1";
    public static final String ex = "switch";

 dib = (Switch) findViewById(R.id.switch1);
        sharedPreferences = getSharedPreferences("dib",MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();
        dib.setChecked(sharedPreferences.getBoolean(ex,false));
        dib.setonCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                if (isChecked) {
                    editor.putBoolean(ex,true);
                } else {
                    editor.putBoolean(ex,false);
                }
                editor.commit();
            }
        });

我需要知道如何使用开关按钮做一些类似于我使用微调器所做的事情:

spipol = (Spinner) findViewById (R.id.spipol);
       String [] options = {"","Short assignment","Workshop","Study","Questions","Review"};
       ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_dropdown_item,options);
       spipol.setAdapter (adapter);
   }

我在互联网上搜索了如何做到这一点,但在我看到的所有地方,他们都使用了保存按钮,我想知道我是否可以在不需要实现此按钮的情况下执行此操作。

我试过了,但没有用:

 spipol.setonItemSelectedListener (new AdapterView.OnItemSelectedListener () {
            @Override
            public void onItemSelected (AdapterView <?> parent,View view,int position,long id) {
                CaptureSpinner = (String) parent.getItemAtPosition (position);
                index = position;
                System.out.println ("Index:" + index);
            }

            @Override
            public void onnothingSelected (AdapterView <?> parent) {
            }
        });

    }
public void savePreference (Context context,int index) {
    SharedPreferences sharpref = getPreferences (getApplicationContext (). MODE_PRIVATE);
    SharedPreferences.Editor editor = sharpref.edit ();
    editor.putInt ("Data",index);
    System.out.println ("Index:" + index);
    editor.apply ();
}

}

解决方法

首先,您需要将 savePreference() 更改为以下代码。您不需要传递上下文或位置。您可以直接从 Spinner 中选择项目,以便能够在 onStop()

上调用此方法
 public void savePreference() {
        SharedPreferences sharpref = getSharedPreferences("Spinner",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharpref.edit();
        editor.putInt("Data",spipol.getSelectedItemPosition());
        System.out.println("Index:" + spipol.getSelectedItemPosition());
        editor.apply();
    }

onStope() 方法中调用上面的方法:

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

在您的 onCreate() 上,您只需调用它:

 spipol = findViewById(R.id.spipol);
        String[] options = {"","Short assignment","Workshop","Study","Questions","Review"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,options);
        spipol.setAdapter(adapter);

        // here to restore the last selected item 
        SharedPreferences sharpref = getSharedPreferences("Spinner",MODE_PRIVATE);
        spipol.setSelection(sharpref.getInt("Data",0));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。