微调框无法保存所选值

时间:2018-10-07 22:27:24

标签: java android spinner

在告诉您之前,我告诉过您,我一直在搜索stackoverflow和许多网络专家中的所有问题,因此请不要将其标记为重复或任何负面行为。我已经尝试,努力工作,但仍然停留在这里很多天。我需要你解决一个简单的问题。

我有一个关于微调器的问题。我尝试使用共享首选项保存默认值,它可以正常工作。但是,当我每次尝试保存选定的微调器值时,它都会失败,每当返回上一页时,我都无法检索到之前选择的值。

FontSettings.java

public class FontSettings extends AppCompatActivity {

private Spinner spinner1, spinnerLatin;
private SharedPreferences mMyPrefs;
private SharedPreferences.Editor mMyEdit;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_font);

    // toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //this line shows back button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //Display data size teks arab in dropdown list spinner
    Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
    spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_with_background);
    spinnerBackgroundChange.setAdapter(spinnerArrayAdapter);
    //save selected spinner value
    SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor=sharedPref.edit();
    editor.putInt("spinnerValue", spinnerBackgroundChange.getSelectedItemPosition());
    editor.apply();

    //Display data size teks latin in dropdown list spinner
    Spinner spinnerLatin = (Spinner)findViewById(R.id.spinnerLatin);
    ArrayAdapter<CharSequence> spinnerArrayLatin = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
    spinnerArrayLatin.setDropDownViewResource(R.layout.textview_with_background);
    spinnerLatin.setAdapter(spinnerArrayLatin);
    // spinnerLatin default value
    spinnerLatin.setSelection(1);

    addListenerOnSpinnerItemSelection();
    addListenerOnSpinner2ItemSelection();
}

public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new SizeArabFont());
}

public void addListenerOnSpinner2ItemSelection() {
    spinnerLatin = (Spinner) findViewById(R.id.spinnerLatin);
    spinnerLatin.setOnItemSelectedListener(new SizeLatinFont());
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
} }

SizeArabFont.java

public class SizeArabFont implements OnItemSelectedListener {

private SharedPreferences.Editor mMyEdit;

public void onItemSelected (AdapterView <?> parent, View view, int pos, long id){

    ((TextView) view).setTextColor(Color.parseColor("#226169"));

    switch(pos) {
        case 0:
            Activity activity = (Activity) parent.getContext();
            TextView dgs = (TextView) activity.findViewById(R.id.tekzArab);
            dgs.setTextSize(12);
            break;

        case 1:
            Activity activitys = (Activity) parent.getContext();
            TextView dgf = (TextView) activitys.findViewById(R.id.tekzArab);
            dgf.setTextSize(14);
            break;

        default:
            Activity activityu = (Activity) parent.getContext();
            TextView dgx = (TextView) activityu.findViewById(R.id.tekzArab);
            dgx.setTextSize(24);
            break;

    }
}

@Override
public void onNothingSelected (AdapterView <?> arg0){
    // TODO Auto-generated method stub
} }

SettingsFont.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reldoa"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
android:background="@android:color/white"
android:orientation="vertical">

        <TextView
            android:id="@+id/sizedoa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:paddingBottom="10dp"
            android:paddingLeft="15dp"
            android:paddingTop="5dp"
            android:text="Ukuran Font"
            android:textColor="#222222"
            android:textSize="18sp" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true" />
  </RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reldoa"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
android:background="@android:color/white"
android:orientation="vertical">

<TextView
    android:id="@+id/sizelatin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:paddingBottom="10dp"
    android:paddingLeft="15dp"
    android:paddingTop="5dp"
    android:text="Jenis Font"
    android:textColor="#222222"
    android:textSize="18sp" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:entries="@array/type_arrays"
    android:prompt="@string/type_font"/>

  </RelativeLayout>

    <!-- Font latin -->

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relLatin"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="35dp"
        android:background="@android:color/white"
        android:orientation="vertical">

        <TextView
            android:id="@+id/fontLatin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:paddingBottom="15dp"
            android:paddingLeft="15dp"
            android:paddingTop="15dp"
            android:text="Font Latin"
            android:textColor="#226169"
            android:textSize="20sp" />

    </RelativeLayout>

非常感谢您的帮助!

[更新代码]使用@Pedro Massango提供的代码更新代码

public class FontSettings extends AppCompatActivity {

private Spinner spinner1, spinnerLatin;
private SharedPreferences mMyPrefs;
private SharedPreferences.Editor mMyEdit;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_font);

    // toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //this line shows back button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //Display data size teks arab in dropdown list spinner
    final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
    spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_with_background);
    spinnerBackgroundChange.setAdapter(spinnerArrayAdapter);

    spinnerBackgroundChange.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=sharedPref.edit();
            editor.putInt("spinnerValue", spinnerBackgroundChange.getSelectedItemPosition());
            editor.apply();

            if (position == 1) {
                int lastSelectedPosition = sharedPref.getInt("spinnerValue", 1);
                spinnerBackgroundChange.setSelection(lastSelectedPosition);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });


    //Display data size teks latin in dropdown list spinner
    Spinner spinnerLatin = (Spinner)findViewById(R.id.spinnerLatin);
    ArrayAdapter<CharSequence> spinnerArrayLatin = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
    spinnerArrayLatin.setDropDownViewResource(R.layout.textview_with_background);
    spinnerLatin.setAdapter(spinnerArrayLatin);
    // spinnerLatin default value
    spinnerLatin.setSelection(1);

    addListenerOnSpinnerItemSelection();
    addListenerOnSpinner2ItemSelection();
}

public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new SizeArabFont());
}

public void addListenerOnSpinner2ItemSelection() {
    spinnerLatin = (Spinner) findViewById(R.id.spinnerLatin);
    spinnerLatin.setOnItemSelectedListener(new SizeLatinFont());
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
} }

2 个答案:

答案 0 :(得分:0)

要将所选项目保存在您的sharedPreferences上,请在您的 onCreate(...)中使用此代码:

final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);
        spinnerBackgroundChange.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sharedPref.edit();
                editor.putInt("spinnerValue", spinnerBackgroundChange.getSelectedItemPosition());
                editor.apply();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

要始终选择微调框上的上一个选定项目,只需在将数据放在微调框上之后调用此代码即可:

 SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);

        final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);

        int lastSelectedPosition = sharedPref.getInt("spinnerValue", 0);
        spinnerBackgroundChange.setSelection(lastSelectedPosition);

请记住,您当前正在保存所选项目的位置,而不是值。

  

更新(下面是新答案)

在下面添加另一个解决方案。我注意到您已将代码放置在错误的位置以更新位置,因此我只是将代码放置在正确的位置(初始化Spinner后)。请尝试一下,让我知道它是否无效。

    public class FontSettings extends AppCompatActivity {

    private Spinner spinner1, spinnerLatin;
    private SharedPreferences mMyPrefs;
    private SharedPreferences.Editor mMyEdit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_font);

        // toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //this line shows back button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //Display data size teks arab in dropdown list spinner
        final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
        spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_with_background);
        spinnerBackgroundChange.setAdapter(spinnerArrayAdapter);

        // Code pasted here
        if (spinnerBackgroundChange.getSelectedItemPosition() == 0) {
            int lastSelectedPosition = sharedPref.getInt("spinnerValue", 0);
            spinnerBackgroundChange.setSelection(lastSelectedPosition);
        }

        spinnerBackgroundChange.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sharedPref.edit();
                editor.putInt("spinnerValue", spinnerBackgroundChange.getSelectedItemPosition());
                editor.apply();

               // Code removed here
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        //Display data size teks latin in dropdown list spinner
        Spinner spinnerLatin = (Spinner)findViewById(R.id.spinnerLatin);
        ArrayAdapter<CharSequence> spinnerArrayLatin = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
        spinnerArrayLatin.setDropDownViewResource(R.layout.textview_with_background);
        spinnerLatin.setAdapter(spinnerArrayLatin);
        // spinnerLatin default value
        spinnerLatin.setSelection(1);

        addListenerOnSpinnerItemSelection();
        addListenerOnSpinner2ItemSelection();
    }

    public void addListenerOnSpinnerItemSelection() {
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new SizeArabFont());
    }

    public void addListenerOnSpinner2ItemSelection() {
        spinnerLatin = (Spinner) findViewById(R.id.spinnerLatin);
        spinnerLatin.setOnItemSelectedListener(new SizeLatinFont());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    } 
}

答案 1 :(得分:0)

我使用此代码,它对我有用。 要保存选定的微调器值:

int main(void) {
  unsigned int_count = 0;
  int scan_count;
  int value;
  while ((scan_count = get_int(&value)) == 1) {
    printf("%u: %d\n", ++int_count, value);
  }
  switch (scan_count) {
    case '\n': printf("Normal end of line.\n"); break;
    case EOF: printf("Normal EOF.\n"); break;
    case 0: printf("Offending character code %d encountered.\n", fgetc(stdin)); break;
  }
}

并加载或检索选定的微调器值:

final SharedPreferences sharedPref = getSharedPreferences("My_Prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor0 = sharedPref.edit();
                    editor0.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor0.commit();